__set__ method is not called for class attribute access
Peter Otten
__peter__ at web.de
Fri Aug 5 09:00:37 EDT 2011
Duncan Booth wrote:
> The descriptor protocol only works when a value is being accessed or set
> on an instance and there is no instance attribute of that name so the
> value is fetched from the underlying class.
Unlike normal class attributes a descriptor is not shaded by an instance
attribute:
>>> class A(object):
... def set_x(self, value): self.__dict__["x"] = value/2.0
... def get_x(self): return self.__dict__["x"] * 2.0
... x = property(get_x, set_x)
...
>>> a = A()
>>> a.x = 42
>>> a.__dict__["x"]
21.0
>>> a.x
42.0
>>> A.x = "something completely different"
>>> a.x
21.0
More information about the Python-list
mailing list