different instances with different data descriptors with the same name
__peter__ at web.de
Mon Feb 18 12:36:38 CET 2008
Fabrizio Pollastri wrote:
> Data descriptors are set as attributes of object types. So if one has many
> instances of the same class and wants each instance to have a different
> property (data descriptor) that can be accessed with a unique attribute
> name, it seems to me that there is no solution with data descriptors.
> There is any workaround to this? Thank in advance for any help.
You can invent a naming convention and then invoke the getter/setter
>>> class A(object):
... def __getattr__(self, name):
... if not name.startswith("_prop_"):
... return getattr(self, "_prop_" + name).__get__(self)
... raise AttributeError(name)
>>> a = A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __getattr__
File "<stdin>", line 5, in __getattr__
>>> a._prop_p = property(lambda self: self.x * self.x)
>>> a.x = 42
But what are you really trying to do?
More information about the Python-list