8000 [3.9] Add more tests to the descriptor howto guide (GH-23506) by miss-islington · Pull Request #23510 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 [3.9] Add more tests to the descriptor howto guide (GH-23506) #23510

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

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,15 @@ the lookup or update:


An interactive session shows that all access to the managed attribute *age* is
logged, but that the regular attribute *name* is not logged::
logged, but that the regular attribute *name* is not logged:

.. testcode::
:hide:

import logging, sys
logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)

.. doctest::

>>> mary = Person('Mary M', 30) # The initial age update is logged
INFO:root:Updating 'age' to 30
Expand Down Expand Up @@ -256,7 +264,15 @@ we call :func:`vars` to look up the descriptor without triggering it:
>>> vars(vars(Person)['age'])
{'public_name': 'age', 'private_name': '_age'}

The new class now logs access to both *name* and *age*::
The new class now logs access to both *name* and *age*:

.. testcode::
:hide:

import logging, sys
logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)

.. doctest::

>>> pete = Person('Peter P', 10)
INFO:root:Updating 'name' to 'Peter P'
Expand Down Expand Up @@ -433,7 +449,9 @@ Here's how the data validators can be used in a real class:
self.kind = kind
self.quantity = quantity

The descriptors prevent invalid instances from being created::
The descriptors prevent invalid instances from being created:

.. doctest::

>>> Component('Widget', 'metal', 5) # Blocked: 'Widget' is not all uppercase
Traceback (most recent call last):
Expand Down Expand Up @@ -1227,7 +1245,10 @@ Now a new dictionary of unique keys can be constructed like this:

.. doctest::

>>> Dict.fromkeys('abracadabra')
>>> d = Dict.fromkeys('abracadabra')
>>> type(d) is Dict
True
>>> d
{'a': None, 'b': None, 'r': None, 'c': None, 'd': None}

Using the non-data descriptor protocol, a pure Python version of
Expand Down
0