__set__ method is not called for class attribute access
Duncan Booth
duncan.booth at invalid.invalid
Sun Aug 7 16:52:48 EDT 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
> 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
>
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
mailing list