Last active
June 12, 2016 17:35
-
-
Save larsbratholm/8d019572051e47d9e128238842193885 to your computer and use it in GitHub Desktop.
const example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# const decorator | |
def const(f): | |
def fset(self, value): | |
raise TypeError | |
def fget(self): | |
return f(self) | |
return property(fget, fset) | |
# example with class | |
class Data(object): | |
def __init__(self, x): | |
self._x_init = x | |
@const | |
def x(self): | |
return self._x_init | |
# will produce error | |
x = 5 | |
d = Data(x) | |
d.x = 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment