I would like to add many dummy-properties to a class via a decorator, like this:
def addAttrs(attr_names):def deco(cls):for attr_name in attr_names:def getAttr(self):return getattr(self, "_" + attr_name)def setAttr(self, value):setattr(self, "_" + attr_name, value)prop = property(getAttr, setAttr)setattr(cls, attr_name, prop)setattr(cls, "_" + attr_name, None) # Default value for that attributereturn clsreturn deco@addAttrs(['x', 'y'])
class MyClass(object):pass
Unfortunately, the decoarator seems to keep the reference of attr_name
instead of its content. Therefore, MyClass.x
and MyClass.y
access both MyClass._y
:
a = MyClass()
a.x = 5
print a._x, a._y
>>> None, 5
a.y = 8
print a._x, a._y
>>> None, 8
What do I have to change to get the expected behavior?