__set__ method is not called for class attribute access
duncan.booth at invalid.invalid
Sun Aug 7 22:52:48 CEST 2011
Peter Otten <__peter__ at web.de> wrote:
> 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
>>>> 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.x = "something completely different"
True, and in fact that's why your suggestion of using two descriptors, one
at each level works. Thanks for pointing out my mistake.
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list