| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -823,12 +823,18 @@ An attribute reference is a primary followed by a period and a name: | |
|
|
||
| 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. This production can be customized by | ||
| overriding the :meth:`__getattr__` method. If this attribute is not available, | ||
| the exception :exc:`AttributeError` is raised. Otherwise, the type and value of | ||
| the object produced is determined by the object. Multiple evaluations of the | ||
| same attribute reference may yield different objects. | ||
|
|
||
| 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:`~object.__getattribute__` method or the :meth:`~object.__getattr__` | ||
| method. The :meth:`!__getattribute__` method is called first and either | ||
| returns a value or raises :exc:`AttributeError` if the attribute is not | ||
| available. | ||
|
Comment thread
Comment on lines
+832
to
+834
Copy link
Copy Markdown
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityOf course, it is possible to create badly behaved objects that raise other exceptions from __getattribute__: >>> class Foo:
... def __getattribute__(self, attr):
... if attr == 'foo':
... raise TypeError('no')
... return object.__getattribute__(self, attr)
...
>>> f = Foo()
>>> f.a = 1
>>> f.a
1
>>> f.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
f.b
File "<stdin>", line 5, in __getattribute__
return object.__getattribute__(self, attr)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Foo' object has no attribute 'b'
>>> f.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
f.foo
File "<stdin>", line 4, in __getattribute__
raise TypeError('no')
TypeError: noDoes that need to be mentioned here? Or is it too obscure?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNo need. Any code can raise a weird error at any time but we don't need to mention that in the docs in the thousands of places where this could occur: This wording corresponds to what the code actually does: def getattr_hook(obj, name):
"Emulate slot_tp_getattr_hook() in Objects/typeobject.c"
try:
return obj.__getattribute__(name)
except AttributeError:
if not hasattr(type(obj), '__getattr__'):
raise
return type(obj).__getattr__(obj, name)
Sorry, something went wrong.
AlexWaygood reacted with thumbs up emoji
All reactions
|
||
|
|
||
| If an :exc:`AttributeError` is raised and the object has a :meth:`!__getattr__` | ||
| method, that method is called as a fallback. | ||
|
|
||
| .. _subscriptions: | ||
|
|
||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI think "or" is fine. The "and/or" is correct but looks weird and doesn't read well.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI disagree that it looks weird, but won't insist
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.