Created
May 9, 2012 03:40
-
-
Save bluven/2641639 to your computer and use it in GitHub Desktop.
getter setter
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
def attr(name): | |
n = '__' + name | |
def fget(self): | |
print 'get' | |
return self.__dict__[n] | |
def fset(self, x): | |
print 'set' | |
self.__dict__[n] = x | |
tmp = locals() | |
del tmp['name'] | |
del tmp['n'] | |
return property(**tmp) | |
def attr2(name): | |
n = '__' + name | |
def fget(self): | |
print 'get' | |
return self.__dict__[n] | |
def fset(self, x): | |
print 'set' | |
self.__dict__[n] = x | |
return property(fget, fset) | |
class X(object): | |
def __init__(self): | |
pass | |
y = attr('y') | |
x = X() | |
x.y = 1 | |
print x.y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment