5

Accessing classmethod of a class always return a different object. This is unlike instance or static methods.

class Foo(object):
    @classmethod
    def foo_classmethod(cls):
        pass

    def foo_instancemethod(self):
        pass

    @staticmethod
    def foo_staticmethod():
        pass

and when tried to compare

In [37]: print(Foo.foo_instancemethod is Foo.foo_instancemethod)
True

In [38]: print(Foo.foo_staticmethod is Foo.foo_staticmethod)
True

In [39]: print(Foo.foo_classmethod is Foo.foo_classmethod)
False

This behavior is same in Python 2 and 3. Does this look like a bug? I came across this while pickling a classmethod in Python3 and doing the is check on the unpickled object.

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
Saim Raza
  • 1,420
  • 13
  • 16
  • This is normal and expected. Note, instance methods do this too, when accessed via an instance. – juanpa.arrivillaga Nov 19 '19 at 14:15
  • 2
    So, this explains what's going on, although it doesn't address `classmethod` specifically, and there are no unbound methods in Python 3: https://stackoverflow.com/questions/15977808/why-dont-methods-have-reference-equality – juanpa.arrivillaga Nov 19 '19 at 14:18

0 Answers0