FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reference Manual 6.3.1: wrong method mentioned · Issue #112331 · python/cpython · GitHub

Repository navigation

Reference Manual 6.3.1: wrong method mentioned #112331

Closed

Description

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

Activity

  1. aichernig commented on Nov 24, 2023

    Author

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

  2. rhettinger commented on Nov 24, 2023

    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.
    
  3. added a commit that references this issue on Nov 24, 2023
  4. aichernig commented on Nov 24, 2023

    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".

  5. added a commit that references this issue on Nov 25, 2023
  6. added 2 commits that reference this issue on Nov 25, 2023
  7. added 2 commits that reference this issue on Nov 25, 2023
  8. added a commit that references this issue on Feb 11, 2024
  9. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL