[Tutor] @property for old style classes vs new style classes

Steven D'Aprano steve at pearwood.info
Thu Sep 15 12:50:46 EDT 2016


On Thu, Sep 15, 2016 at 02:08:12PM +0000, eryk sun wrote:

> Getting attributes also prefers the instance dict. However, to support
> bound methods (e.g. __init__), it falls back on a class lookup and
> calls the descriptor __get__ method if defined. 

Is that documented anywhere? When was it introduced?

Because I can see that in Python 2.4 and higher, function objects have a 
__get__, and old-style classes appear to call __get__ methods. But going 
back to Python 1.5 function objects DON'T have a __get__ and attribute 
lookup ignores __get__ even if designed.

>>> class MyDescriptor:
...     def __get__(self):
...             print "calling descriptor __get__"
...             return lambda self: "the getter"
...
>>> class X:
...     desc = MyDescriptor()
...
>>> X.desc
<__main__.MyDescriptor instance at 82d1940>


Some time between Python 1.5 and 2.4, the behaviour of old-style classes 
was changed to *half* support the descriptor protocol. As far as I can 
see, that's not mentioned in the Descriptor HowTo guide:

https://docs.python.org/2/howto/descriptor.html

and it contradicts the comment here:

https://docs.python.org/2/reference/datamodel.html#invoking-descriptors

Quote:

    Note that descriptors are only invoked for new style objects or 
    classes (ones that subclass object() or type()).




-- 
Steve


More information about the Tutor mailing list