Skip to content

Instantly share code, notes, and snippets.

@bluven
Created May 9, 2012 03:40
Show Gist options
  • Save bluven/2641639 to your computer and use it in GitHub Desktop.
Save bluven/2641639 to your computer and use it in GitHub Desktop.
getter setter
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