[issue47007] [doc] str docs are inconsistent with special method lookup
New submission from Vanshaj Singhania <svanshaj2001@gmail.com>: 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 ---------- assignee: docs@python components: Documentation messages: 415110 nosy: docs@python, itsvs priority: normal severity: normal status: open title: [doc] str docs are inconsistent with special method lookup type: enhancement versions: Python 3.10, Python 3.11, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Change by Vanshaj Singhania <svanshaj2001@gmail.com>: ---------- keywords: +patch pull_requests: +29961 stage: -> patch review pull_request: https://github.com/python/cpython/pull/31863 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Vedran Čačić <vedgar@gmail.com> added the comment: You mean `type(object).__str__(object)` instead of `type(object).__str__()`, obviously. ---------- nosy: +veky _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- nosy: +miss-islington nosy_count: 4.0 -> 5.0 pull_requests: +30379 pull_request: https://github.com/python/cpython/pull/32318 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Jelle Zijlstra <jelle.zijlstra@gmail.com> added the comment: New changeset bb86d1d9fbd1888524e04475383f4ea764277f67 by Vanshaj Singhania in branch 'main': bpo-47007: [doc] `str` special method lookup (GH-31863) https://github.com/python/cpython/commit/bb86d1d9fbd1888524e04475383f4ea7642... ---------- nosy: +JelleZijlstra _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +30380 pull_request: https://github.com/python/cpython/pull/32319 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
Jelle Zijlstra <jelle.zijlstra@gmail.com> added the comment: Thanks for the report and fix! ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/f502dadb332f911fa3b6c531bbc5065795c... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/c0063bdc7b5fed98c6799f3da0a954a775e... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue47007> _______________________________________
participants (5)
-
Jelle Zijlstra
-
miss-islington
-
Raymond Hettinger
-
Vanshaj Singhania
-
Vedran Čačić