Skip to content

Reference Manual 6.3.1: wrong method mentioned #112331

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

Closed
aichernig opened this issue Nov 23, 2023 · 3 comments
Closed

Reference Manual 6.3.1: wrong method mentioned #112331

aichernig opened this issue Nov 23, 2023 · 3 comments
Labels
docs Documentation in the Doc dir

Comments

@aichernig
Copy link

aichernig commented Nov 23, 2023

Documentation

6.3.1. Attribute references

This section states that "This production can be customized by overriding the __getattr__() method."
This is wrong and should be replaced with "This production can be customized by overriding the __getattribute__(self, attr) method.".

__getattr__() is only called when the attribute is not found. __getattribute__(self, attr) is the method that is called when accessing an attribute. Here is an example code for both:

class C:
    def __init__(self):
        self.a = 1
    def __getattr__(self, attr): # called when attribute not found
        print('attribute does not exist')
    def __getattribute__(self, attr): # called first
        print('attribute', attr, 'accessed')
        return object.__getattribute__(self, attr) # object is the top-level type for classes

o = C()
print(o.a) # found, calls __getattribute__
print(o.b)  # not found, calls both methods.

Hence, either mention both methods or only __getattribute__.

Linked PRs

@aichernig aichernig added the docs Documentation in the Doc dir label Nov 23, 2023
@aichernig
Copy link
Author

What do you mean? I explained what the problem is and how it should be resolved.

@rhettinger
Copy link
Contributor

@aichernig Will this revised text suffice?

The primary must evaluate to an object of a type that supports attribute
references, which most objects do.  This object is then asked to produce the
attribute whose name is the identifier. The type and value produced is
determined by the object.  Multiple evaluations of the same attribute
reference may yield different objects.

This production can be customized by overriding the :meth:`__getattribute__`
method or the :meth:`__getattr__` method.  The :meth:`__getattribute__`
is called first and either returns a value or raises :exc:`AttributeError`
if the attribute is not available.

If an :exc:`AttributeError` raised and the object has a :meth:`__getattr__`
method, that method is called as a fallback.

rhettinger added a commit to rhettinger/cpython that referenced this issue Nov 24, 2023
@aichernig
Copy link
Author

Yes almost perfect. I think the last sentence "If an :exc:AttributeError raised ... " should be "If an :exc:AttributeError is raised ..." or "was raised".

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 25, 2023
… mechanics (pythongh-112375)

(cherry picked from commit 97f8f28)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 25, 2023
… mechanics (pythongh-112375)

(cherry picked from commit 97f8f28)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 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
Projects
None yet
Development

No branches or pull requests

2 participants