Skip to content

Methods, classmethods and staticmethods #233

Closed
@Jongy

Description

@Jongy

The effects of descriptors on comparison and identity of methods:

class C:
    def m(self):
        return 1
    @classmethod
    def cm(cls):
        return 2
    @staticmethod
    def sm():
        return 3

# compare methods on the class object
print(C.m == C.m)  # True
print(C.m is C.m)  # True

# compare classmethods on the class object
print(C.cm == C.cm)  # True
print(C.cm is C.cm)  # False ??

# compare staticmethods on the class object
print(C.sm == C.sm)  # True
print(C.sm is C.sm)  # True


c1 = C()
c2 = C()

# compare methods on instances
print(c1.m == c2.m)  # False, makes sense
print(c1.m is c1.m)  # False ??

# compare classmethods on all
print(c1.cm == c2.cm)  # True
print(c1.cm is c2.cm)  # False again

# staticmethods again
print(c1.sm is c2.sm is C.sm)  # always True...

with some more examples in this SO answer.

I can post a PR summarizing it.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions