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

gh-119016: Remove outdated sentences from the "classes" tutorial #119130

Merged
merged 7 commits into from
Jun 1, 2024

Conversation

nineteendo
Copy link
Contributor

@nineteendo nineteendo commented May 17, 2024

@nineteendo nineteendo marked this pull request as ready for review May 17, 2024 20:48
@eendebakpt
Copy link
Contributor

I am still wondering why the text "not referring to..." is needed here. There are some differences between class instance methods and builtin class instance methods (e.g. alist.append does not have a __func__ attribute, the type is different), but those differences are not mentioned in the text below.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Doc/tutorial/classes.rst Outdated Show resolved Hide resolved
@nineteendo
Copy link
Contributor Author

https://docs.python.org/3/tutorial/classes.html#odds-and-ends:

Instance method objects have attributes, too: m.__self__ is the instance object with the method m(), and m.__func__ is the function object corresponding to the method.

Doc/tutorial/classes.rst Outdated Show resolved Hide resolved
Comment on lines 340 to 344
The other kind of instance attribute reference is a *method*. In the following
discussion, we’ll use the term method exclusively to mean methods of class
instance objects, unless explicitly stated otherwise. (In other words, we are
not referring to built-in methods such as ``alist.append`` where ``alist`` is
a :class:`list` object.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph is very old. It appears to have remained in this tutorial largely unchanged since it was originally added in 1992, in 5e0759d.

I don't think that the original purpose of this paragraph was to point out that alist.append has, strictly speaking, a slightly different type to a user-defined method on a user-defined class. That feels like a subtle point that leads to few visible differences in behaviour for users.

In very early versions of Python, many builtin types such as list were not classes at all -- they were more primitive types that operated according to a different object model. (See https://stackoverflow.com/a/54873/13990016 for more details. This changed in Python 2.2, originally released in 2001.) I think this paragraph was originally intended to clarify that even objects that were not instances of classes, such as lists, could also have methods.

Nowadays, all objects are instances of classes, and it is not possible to create types that do not inherit from object. It's been this way for a long time now. As such, I think this paragraph is now largely redundant. Rather than making the edit you propose here, I would simply delete most of this paragraph altogether.

Suggested change
The other kind of instance attribute reference is a *method*. In the following
discussion, we’ll use the term method exclusively to mean methods of class
instance objects, unless explicitly stated otherwise. (In other words, we are
not referring to built-in methods such as ``alist.append`` where ``alist`` is
a :class:`list` object.)
The other kind of instance attribute reference is a *method*. A method is a
function that "belongs to" an object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about suggesting this originally, but https://docs.python.org/3/reference/datamodel.html#built-in-methods makes it sound like there's still some difference between built-in methods and methods on user defined classes (besides built-ins being defined in C that is). As mentioned in #119016 (comment), the types are different at least.

Does the last paragraphs in particular of both 9.3.4 (https://docs.python.org/3/tutorial/classes.html#method-objects) and 9.7 (https://docs.python.org/3/tutorial/classes.html#odds-and-ends) apply equally to built-in methods? If not, there might still be some confusion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, builtin methods are a different type to pure-Python methods, but they're meant to be entirely duck-type compatible. Any glaring differences between them are arguably bugs. Off the top of my head, I don't think any of the paragraphs you link to there are different when it comes to builtin methods.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AlexWaygood AlexWaygood added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Jun 1, 2024
@AlexWaygood AlexWaygood changed the title gh-119016: Clarify classes tutorial gh-119016: Remove outdated sentences from the "classes" tutorial Jun 1, 2024
@AlexWaygood AlexWaygood enabled auto-merge (squash) June 1, 2024 21:03
@AlexWaygood AlexWaygood merged commit c618f7d into python:main Jun 1, 2024
27 checks passed
@miss-islington-app

This comment was marked as outdated.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 1, 2024
pythonGH-119130)

(cherry picked from commit c618f7d)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Jun 1, 2024

GH-119925 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 1, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 1, 2024
pythonGH-119130)

(cherry picked from commit c618f7d)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Jun 1, 2024

GH-119926 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Jun 1, 2024
AlexWaygood added a commit that referenced this pull request Jun 1, 2024
…al (GH-119130) (#119926)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this pull request Jun 1, 2024
…al (GH-119130) (#119925)

Co-authored-by: Nice Zombies <nineteendo19d0@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@nineteendo nineteendo deleted the clarify-classes-tutorial branch June 2, 2024 04:53
barneygale pushed a commit to barneygale/cpython that referenced this pull request Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants