Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 755c6e6

Browse files
authored
Minor readability improvements. Also note performance impact of __slots__. (GH-24456)
1 parent d9dda32 commit 755c6e6

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

Doc/howto/descriptor.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ add new capabilities one by one.
4242
Simple example: A descriptor that returns a constant
4343
----------------------------------------------------
4444

45-
The :class:`Ten` class is a descriptor that always returns the constant ``10``
46-
from its :meth:`__get__` method:
45+
The :class:`Ten` class is a descriptor whose :meth:`__get__` method always
46+
returns the constant ``10``:
4747

4848
.. testcode::
4949

@@ -70,10 +70,10 @@ and descriptor lookup:
7070
>>> a.y # Descriptor lookup
7171
10
7272

73-
In the ``a.x`` attribute lookup, the dot operator finds the key ``x`` and the
74-
value ``5`` in the class dictionary. In the ``a.y`` lookup, the dot operator
75-
finds a descriptor instance, recognized by its ``__get__`` method, and calls
76-
that method which returns ``10``.
73+
In the ``a.x`` attribute lookup, the dot operator finds ``'x': 5``
74+
in the class dictionary. In the ``a.y`` lookup, the dot operator
75+
finds a descriptor instance, recognized by its ``__get__`` method.
76+
Calling that method returns ``10``.
7777

7878
Note that the value ``10`` is not stored in either the class dictionary or the
7979
instance dictionary. Instead, the value ``10`` is computed on demand.
@@ -300,7 +300,7 @@ used in cases where a descriptor needs to know either the class where it was
300300
created or the name of class variable it was assigned to. (This method, if
301301
present, is called even if the class is not a descriptor.)
302302

303-
Descriptors get invoked by the dot "operator" during attribute lookup. If a
303+
Descriptors get invoked by the dot operator during attribute lookup. If a
304304
descriptor is accessed indirectly with ``vars(some_class)[descriptor_name]``,
305305
the descriptor instance is returned without invoking it.
306306

@@ -1380,7 +1380,10 @@ takes 48 bytes with ``__slots__`` and 152 bytes without. This `flyweight
13801380
design pattern <https://en.wikipedia.org/wiki/Flyweight_pattern>`_ likely only
13811381
matters when a large number of instances are going to be created.
13821382

1383-
4. Blocks tools like :func:`functools.cached_property` which require an
1383+
4. Improves speed. Reading instance variables is 35% faster with
1384+
``__slots__`` (as measured with Python 3.10 on an Apple M1 processor).
1385+
1386+
5. Blocks tools like :func:`functools.cached_property` which require an
13841387
instance dictionary to function correctly:
13851388

13861389
.. testcode::

0 commit comments

Comments
 (0)