Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods, classmethods and staticmethods #233

Closed
Jongy opened this issue Oct 24, 2020 · 1 comment · Fixed by #241
Closed

Methods, classmethods and staticmethods #233

Jongy opened this issue Oct 24, 2020 · 1 comment · Fixed by #241

Comments

@Jongy
Copy link
Contributor

Jongy commented Oct 24, 2020

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.

@satwikkansal
Copy link
Owner

That's interesting. We can possibly add following based on the SO answer you linked,

c1.__str__ == c2.__str__  # False
c1.__dict__.__str__ == c2.__dict__.__str__ # True ??

Looking forward to your PR :)

Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 2, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 3, 2020
Jongy added a commit to Jongy/wtfpython that referenced this issue Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants