This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Steven.Barker
Recipients Steven.Barker
Date 2014-04-30.02:26:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The "repr" of bound method objects can be misleading in certain situations. The repr is always is of the format:

    <bound method x.y of <object>>

But "x" is often incorrect.

Here are some examples where the current code gets it wrong:

    # inherited method
    class Base(object):
        def foo(self):

    class Derived(Base):

    # not too bad, prints "<bound method of <__main__.Derived object at 0xXXXX>>"
    # it probably should say "" instead of "", but at least they're two names for the same function

    # however, an override and super() it gets very bad
    class Derived2(Base):
        def foo(self):

    print(super(Derived2, Derived2()).foo)
    # totally wrong, prints "<bound method of __main__.derived2 object at 0xXXXX>>"
    # but it actually *is* bound to a Derived2 instance!

    # bound class methods:
    class Test(object):
        def foo(cls):

    # wrong, prints <bound method of <class '__main__.Test'>>

I suggest that rather than trying to assemble the "x.y" pair by from "__self__.__class__" and "__func__.__name__", the BoundMethod should just use the "__func__.__qualname__". In each of the cases above, the function's location would be correctly located this way.

I came across this bug while investigating a confusing (to me) issue with metaclasses and inheritance. The misleading "repr" output made it much harder to figure out that my expectations were wrong. Here's a simplified example of how it led me astray:

    class A(object):
        def foo(cls):
            return "classmethod from A"

    class BMeta(type):
        def foo(cls):
            return "instancemethod from BMeta"

    class B(A, metaclass=BMeta):

    print( # surprisingly (to me) prints "classmethod from A"
    print(   # incorrectly prints "<bound method of <class __main__.B>>"

It is presumably not a bug that inherited class methods take precedence over instance methods from a metaclass (though it was surprising to me at the time). The repr of the bound method though, suggested exactly the opposite. Given that it gets many more common situations wrong as well, I think that the repr should be fixed.

The relevant code appears to be in the method_repr function in Objects/Classobject.c .
Date User Action Args
2014-04-30 02:26:41Steven.Barkersetrecipients: + Steven.Barker
2014-04-30 02:26:41Steven.Barkersetmessageid: <>
2014-04-30 02:26:41Steven.Barkerlinkissue21389 messages
2014-04-30 02:26:39Steven.Barkercreate