Skip to content

Improve discussion about how __getattr__ is invoked. #31435

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

Merged
merged 39 commits into from
Feb 20, 2022
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
fb6744d
merge
rhettinger May 6, 2021
7f21a1c
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 15, 2021
7da42d4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Aug 25, 2021
e31757b
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
f058a6f
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
1fc29bd
Merge branch 'main' of github.com:python/cpython
rhettinger Sep 4, 2021
e5c0184
Merge branch 'main' of github.com:python/cpython
rhettinger Oct 30, 2021
3c86ec1
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
96675e4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Nov 9, 2021
de558c6
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
61bca51
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
418a07f
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
ea23a8b
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 21, 2021
ba248b7
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 27, 2021
6a3b1ca
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 28, 2021
9bc1df1
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
d4466ba
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
014a352
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 5, 2021
a89f02e
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 8, 2021
aae9a5f
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 10, 2021
761d99e
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Dec 17, 2021
da9bbe5
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 31, 2021
7ba634b
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 1, 2022
1dba221
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 4, 2022
e1a7b27
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 5, 2022
56e41a3
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Jan 8, 2022
9736650
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 27, 2022
3120a34
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 28, 2022
600be63
Merge branch 'main' of github.com:python/cpython
rhettinger Feb 6, 2022
e4efe68
Improve wording and accuracy of the __getattr__ wording
rhettinger Feb 20, 2022
a65c47a
Add role markup for __getattr__
rhettinger Feb 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,14 @@ a pure Python equivalent:
>>> b.g == b['g'] == ('getattr_hook', b, 'g')
True

Note, there is no :meth:`__getattr__` hook in the :meth:`__getattribute__`
code. That is why calling :meth:`__getattribute__` directly or with
``super().__getattribute__`` will bypass :meth:`__getattr__` entirely.

Interestingly, attribute lookup doesn't call :meth:`object.__getattribute__`
directly. Instead, both the dot operator and the :func:`getattr` function
perform attribute lookup by way of a helper function:
Instead, it is the dot operator and the :func:`getattr` function that are
responsible for invoking :meth:`__getattr__` whenever :meth:`__getattribute__`
raises an :exc:`AttributeError`. Their logic is encapsulated in a helper
function:

.. testcode::

Expand Down Expand Up @@ -744,12 +748,6 @@ perform attribute lookup by way of a helper function:
...
AttributeError: 'ClassWithoutGetAttr' object has no attribute 'z'

So if :meth:`__getattr__` exists, it is called whenever :meth:`__getattribute__`
raises :exc:`AttributeError` (either directly or in one of the descriptor calls).

Also, if a user calls :meth:`object.__getattribute__` directly, the
:meth:`__getattr__` hook is bypassed entirely.


Invocation from a class
-----------------------
Expand Down