Author Justin.Eldridge
Recipients Justin.Eldridge, docs@python
Date 2015-01-25.17:16:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1422206163.24.0.274216799196.issue23317@psf.upfronthosting.co.za>
In-reply-to
Content
The section titled "Invoking Descriptors" in the Python Language Reference [1]
says:

    Class Binding
        If binding to a new-style class, A.x is transformed into the call: 
        A.__dict__['x'].__get__(None, A).

This suggests that __get__ is looked up on the instance of x, when I believe
that it is actually looked up on the type. That is, it's my understanding that
A.x invokes:

    type(A.__dict__['x']).__get__(A.__dict__['x'], None, Foo))

Here's some Python 3.4 code demonstrating this:

    class A:
        pass

    class Descriptor:
        def __get__(self, obj, cls):
            print("Getting!")
            
    A.x = Descriptor()

    def replacement_get(obj, cls):
        print("This is a replacement.")
        
    A.__dict__['x'].__get__ = replacement_get

Now, writing:

    >>> A.x
    Getting!

    >>> A.__dict__['x'].__get__(None, A)
    This is a replacement!

    >>> type(A.__dict__['x']).__get__(A.__dict__['x'], None, A)
    Getting!

The documentation makes a similar statement about instance binding that also
appears to be incorrect. This is the case in all versions of the document I
could find.

What I believe to be the actual behavior is implied by a later section in the
same document, which states that the implicit invocation of special methods is
only guaranteed to work correctly if the special method is defined on the type,
not the instance.  This suggests that the statements in "Invoking Descriptors"
aren't quite correct, and while the true behavior is a little more verbose, I
think it would be worthwhile to update the documentation so as to avoid
confusion.

[1]: https://docs.python.org/2/reference/datamodel.html#invoking-descriptors
History
Date User Action Args
2015-01-25 17:16:03Justin.Eldridgesetrecipients: + Justin.Eldridge, docs@python
2015-01-25 17:16:03Justin.Eldridgesetmessageid: <1422206163.24.0.274216799196.issue23317@psf.upfronthosting.co.za>
2015-01-25 17:16:03Justin.Eldridgelinkissue23317 messages
2015-01-25 17:16:02Justin.Eldridgecreate