-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Classes tutorial: confusing explanation of non-instance methods #119016
Comments
Maybe the confusion is due to the interpretation of "list object". If I read the documentation it seems like "list object" refers to the builtin x=[]
list.append(x, 2) # here append is a method of the list object. but list is not a class instance
print(x)
x.append(3) # here append is a method of the list instance "x"
print(x) The documentation is then confusing because there is only one
should be rewritten to
Making the distinction between instance methods and non-instance methods is relevant for the part that describes method objects, in particular the part |
Thanks @eendebakpt. I've dug deeper into this and actually I think the root issue is that
So in Python, "method" refers to instance attribute used to call the associated function on the instance whether the type is user-defined or built-in. The implementation differs slightly and aspects of these differences are visible to the user so the docs in question only address "instance methods" meaning functions belonging to an instance of a user-defined class (meaning defined in Python rather than C). Note that class methods are a actually a special case of instance methods. Static methods may be implemented similarly (in pure python), but the docs seem to indicate they are in CPython and in any case to the user it looks like they are a regular unbounded function. Additional details at https://docs.python.org/3/howto/descriptor.html#kinds-of-methods. So I propose the following:
|
My analysis of the original intention of this paragraph is slightly different. I don't think it was intended to point out the difference between user-defined methods and methods of builtin types. See my comment at #119130 (comment). |
…9130) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
pythonGH-119130) (cherry picked from commit c618f7d) Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
pythonGH-119130) (cherry picked from commit c618f7d) Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Thanks for the report @deuberger! We've removed the confusing sentences from the docs in the |
python#119130) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
python#119130) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
python#119130) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Documentation
From https://docs.python.org/3/tutorial/classes.html#instance-objects:
From the user's perspective, a list is a class in Python 3. It may be implemented under the hood using some special construct, but in all the documentation and to the user, it is still a class. So an a list object is a class instance for all intents and purposes even if it's really not under the hood.
I suggest reworking this paragraph to clarify whatever it's trying to communicate or deleting it except the first 2 sentences.
A potential reworked version is below, but I'm not sure it wouldn't still be confusing to someone new.
Note that part of the confusion might be because the tutorial doesn't really explain about bound methods. Note that the following section 9.3.4 mentions a method being "bound" without the terminology ever being explained.
Is the distinction between built-in and instance methods even necessary at this point especially in higher level docs like the tutorial?
Linked PRs
The text was updated successfully, but these errors were encountered: