Skip to content

Commit 4b61df5

Browse files
encukoublaisep
andcommitted
More rewording
Co-authored-by: Blaise Pabon <blaise@gmail.com>
1 parent 3706032 commit 4b61df5

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

‎Doc/reference/expressions.rst‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,10 +1288,10 @@ Primaries
12881288
Primaries represent the most tightly bound operations of the language.
12891289
They are:
12901290

1291-
- Bare :ref:`atoms <atoms>`
1292-
- :ref:`Attribute references <attribute-references>`, such as ``math.pi``
1293-
- :ref:`Subscriptions and slicings <subscriptions>`, such as ``names[0]``
1294-
- :ref:`Calls <calls>`, such as ``print()``
1291+
- bare :ref:`atoms <atoms>`,
1292+
- :ref:`attribute references <attribute-references>`, such as ``math.pi``,
1293+
- :ref:`subscriptions and slicings <subscriptions>`, such as ``names[0]``, and
1294+
- :ref:`calls <calls>`, such as ``print()``.
12951295

12961296
The formal syntax of primaries is:
12971297

@@ -1322,12 +1322,10 @@ For example::
13221322

13231323
>>> import math
13241324

1325-
>>> # get the `pi` attribute of the `math` module
1326-
>>> math.pi
1325+
>>> math.pi # get the `pi` attribute of the `math` module
13271326
3.141592653589793
13281327

1329-
>>> # get the `real` part of a complex number
1330-
>>> (3 + 2j).real
1328+
>>> (3 + 2j).real # get the `real` part of a complex number
13311329
3.0
13321330

13331331
At runtime, the primary expression is evaluated, and the result's
@@ -1337,17 +1335,19 @@ By default, :meth:`!__getattribute__` looks attributes up
13371335
in several places, such as the object's :term:`instance dictionary`,
13381336
its type's attributes and descriptors, and the type's
13391337
:meth:`~object.__getattr__` method, if any.
1340-
Typically, this method returns the same object each time it's called
1341-
with a given object and name, or it raises :exc:`AttributeError` if
1342-
the attribute is not found.
1338+
Typically, :meth:`!__getattribute__` raises :exc:`AttributeError` if
1339+
the attribute is not found, and it returns the same object each time
1340+
it's called with a given object and name.
13431341
However, the method is called each time an attribute access is evaluated,
1344-
and it can return a differerent object each time.
1342+
and it can raise different errors or return a differerent object each time.
13451343

1346-
To customize the behavior of attribute references on a class you define,
1347-
it is typically easier to add a descriptor (for example, using :class:`property`)
1348-
or to override :meth:`~object.__getattr__`, rather than override
1349-
:meth:`!__getattribute__` itself.
1350-
See :ref:`attribute-access` for details.
1344+
.. note::
1345+
1346+
When customizing the behavior of attribute references on a class you define,
1347+
consider adding a descriptor (for example, using :class:`property`)
1348+
or overriding :meth:`~object.__getattr__`, rather than replacing the entire
1349+
attribute lookup mechanism by overriding :meth:`!__getattribute__` itself.
1350+
See :ref:`attribute-access` for details.
13511351

13521352
Attribute references may also be used as targets in
13531353
:ref:`assignment <assignment>` or :ref:`deletion <del>` statements.
@@ -1371,6 +1371,11 @@ instead of :meth:`~object.__getattribute__`.
13711371
>>> obj
13721372
namespace()
13731373
1374+
>>> del obj.color # delete an attribute that doesn't exist any more
1375+
Traceback (most recent call last):
1376+
...
1377+
AttributeError: 'types.SimpleNamespace' object has no attribute 'color'
1378+
13741379
13751380
The formal grammar of attribute references is:
13761381

0 commit comments

Comments
 (0)