8000 Add more tests to the descriptor howto guide (GH-23506) · Ringdingcoder/cpython@85c8492 · GitHub
[go: up one dir, main page]

Skip to content

Commit 85c8492

Browse files
authored
Add more tests to the descriptor howto guide (pythonGH-23506)
1 parent 4fedd71 commit 85c8492

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

Doc/howto/descriptor.rst

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,15 @@ the lookup or update:
170170

171171

172172
An interactive session shows that all access to the managed attribute *age* is
173-
logged, but that the regular attribute *name* is not logged::
173+
logged, but that the regular attribute *name* is not logged:
174+
175+
.. testcode::
176+
:hide:
177+
178+
import logging, sys
179+
logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)
180+
181+
.. doctest::
174182

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

259-
The new class now logs access to both *name* and *age*::
267+
The new class now logs access to both *name* and *age*:
268+
269+
.. testcode::
270+
:hide:
271+
272+
import logging, sys
273+
logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True)
274+
275+
.. doctest::
260276

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

436-
The descriptors prevent invalid instances from being created::
452+
The descriptors prevent invalid instances from being created:
453+
454+
.. doctest::
437455

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

12281246
.. doctest::
12291247

1230-
>>> Dict.fromkeys('abracadabra')
1248+
>>> d = Dict.fromkeys('abracadabra')
1249+
>>> type(d) is Dict
1250+
True
1251+
>>> d
12311252
{'a': None, 'b': None, 'r': None, 'c': None, 'd': None}
12321253

12331254
Using the non-data descriptor protocol, a pure Python version of

0 commit comments

Comments
 (0)
0