-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add section on methods equality and identity #241
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @Jongy for the PR. The example is well-thought out and looks pretty good. I just added comments for minor suggestions, feel free to check them out whenver you get time.
And yes, you're right about the __str__
method snippet, makes sense to skip it (I didn't knew it doesn't work with mordern python versions).
>>> print(o1.staticm is o1.staticm is o2.staticm is SomeClass.staticm) | ||
True | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To set up more clearly, can we add a sentence along the lines of
Accessing
classm
ormethod
twice creates "equal but not same" objects for instance ofSomeClass
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, added.
`SomeClass.method is SomeClass.method` is truthy. | ||
* `classmethod` transforms functions into class methods. Class methods are descriptors that, when accessed, create | ||
a method object which binds the *class* (type) of the object, instead of the object itself. | ||
* Unlike functions, `classmethod`s will create a method also when accessed as class attributes (in which case they |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be "Like" instead of "Unlike"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, its the difference between the function descriptor (func_descr_get
) which, when invoked on the class (as opposed to an instace), returns the function itself and not a method; while the classmethod descriptor (cm_descr_get
) will create a method also when invoked directly on a type object (e.g a class).
Perhaps my phrasing isn't clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine, I misread the also part, my bad.
README.md
Outdated
a method object which binds the *class* (type) of the object, instead of the object itself. | ||
* Unlike functions, `classmethod`s will create a method also when accessed as class attributes (in which case they | ||
bind the class, not to the type of it). So `SomeClass.classm is SomeClass.classm` is falsy. | ||
* A method object compares equal when both the functions are equal, and the bound objects are one. So |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity, I think we can say "the bound objects are same" instead of "the bound objects are one".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, reads better.
Thanks, I did take my time organizing it, since it turned out pretty heavy. By the way - I just noticed there already is a section about this behavior, To remove redundancy, perhaps we want to merge them / remove one of them, while keeping all notes? |
Ah, right, I totally forgot about that. But now that I see it, your example is a superset of the existing one (except for the I know merging might make it slightly longer, but it's fine as long as it's interesting and non-redundant. |
I will look into merging it. About repeating the |
Only part that's missing is this:
I'll merge it into my example, alright? |
@satwikkansal Merged them, decided to keep the "new" name since it's more relevant IMO, tell me what you think of this change. |
Looks good now, thanks, merging it! |
Thanks @satwikkansal :) |
@satwikkansal ,
I decided to skip this particular snippet, because:
classmethod
,staticmethod
etc is more useful for Python developers, compared to understanding the (funny)c1.__dict__.__str__
o.OCloses: #233