This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [doc] str docs are inconsistent with special method lookup
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: JelleZijlstra, docs@python, itsvs, miss-islington, rhettinger, veky
Priority: normal Keywords: patch

Created on 2022-03-14 00:40 by itsvs, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31863 merged itsvs, 2022-03-14 04:56
PR 32318 merged miss-islington, 2022-04-05 02:34
PR 32319 merged miss-islington, 2022-04-05 02:34
Messages (6)
msg415110 - (view) Author: Vanshaj Singhania (itsvs) * Date: 2022-03-14 00:40
The documentation for the `str` class[^1] says:

> If neither encoding nor errors is given, str(object) returns object.__str__()

This led our students[^2] to try the following code:

    >>> class Test:
    ...     def __str__(self):
    ...         return 'hi'
    ... 
    >>> test = Test()
    >>> test.__str__ = lambda self: 'bye'
    >>> str(test)

The expected result was calling `test.__str__()`, but instead the interpreter output was `'hi'`.

The docs for special method lookup[^3] do say that instance attributes are ignored:

> For custom classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

This makes sense, and explains the observed behavior, but the `str` class docs seem to be inconsistent with this. Perhaps it would be more clear if the `str` documentation mentioned that it called `type(object).__str__()` instead of `object.__str__()`.

[^1]: https://docs.python.org/3/library/stdtypes.html#str
[^2]: CS 61A at UC Berkeley, https://cs61a.org
[^3]: https://docs.python.org/3/reference/datamodel.html#special-method-lookup
msg415122 - (view) Author: Vedran Čačić (veky) * Date: 2022-03-14 08:28
You mean `type(object).__str__(object)` instead of `type(object).__str__()`, obviously.
msg416718 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-04-05 02:34
New changeset bb86d1d9fbd1888524e04475383f4ea764277f67 by Vanshaj Singhania in branch 'main':
bpo-47007: [doc] `str` special method lookup (GH-31863)
https://github.com/python/cpython/commit/bb86d1d9fbd1888524e04475383f4ea764277f67
msg416723 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2022-04-05 02:37
Thanks for the report and fix!
msg416728 - (view) Author: miss-islington (miss-islington) Date: 2022-04-05 02:56
New changeset f502dadb332f911fa3b6c531bbc5065795cca242 by Miss Islington (bot) in branch '3.10':
bpo-47007: [doc] `str` special method lookup (GH-31863)
https://github.com/python/cpython/commit/f502dadb332f911fa3b6c531bbc5065795cca242
msg416731 - (view) Author: miss-islington (miss-islington) Date: 2022-04-05 02:59
New changeset c0063bdc7b5fed98c6799f3da0a954a775e3d89e by Miss Islington (bot) in branch '3.9':
bpo-47007: [doc] `str` special method lookup (GH-31863)
https://github.com/python/cpython/commit/c0063bdc7b5fed98c6799f3da0a954a775e3d89e
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91163
2022-04-05 02:59:12miss-islingtonsetmessages: + msg416731
2022-04-05 02:56:37miss-islingtonsetmessages: + msg416728
2022-04-05 02:37:36JelleZijlstrasetstatus: open -> closed
resolution: fixed
messages: + msg416723

stage: patch review -> resolved
2022-04-05 02:34:41miss-islingtonsetpull_requests: + pull_request30380
2022-04-05 02:34:39JelleZijlstrasetnosy: + JelleZijlstra
messages: + msg416718
2022-04-05 02:34:36miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request30379
2022-03-14 08:28:20vekysetnosy: + veky
messages: + msg415122
2022-03-14 04:56:23itsvssetkeywords: + patch
stage: patch review
pull_requests: + pull_request29961
2022-03-14 04:07:55rhettingersetnosy: + rhettinger
2022-03-14 00:40:21itsvscreate