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

Skip to content

Commit fd27f62

Browse files
committed
Add cross-reference to typing.NamedTuple. Doctest two more examples.
1 parent f59249b commit fd27f62

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

Doc/library/collections.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,9 @@ field names, the method and attribute names start with an underscore.
850850
.. method:: somenamedtuple._asdict()
851851

852852
Return a new :class:`OrderedDict` which maps field names to their corresponding
853-
values::
853+
values:
854+
855+
.. doctest::
854856

855857
>>> p = Point(x=11, y=22)
856858
>>> p._asdict()
@@ -912,7 +914,9 @@ Since a named tuple is a regular Python class, it is easy to add or change
912914
functionality with a subclass. Here is how to add a calculated field and
913915
a fixed-width print format:
914916

915-
>>> class Point(namedtuple('Point', 'x y')):
917+
.. doctest::
918+
919+
>>> class Point(namedtuple('Point', ['x', 'y'])):
916920
... __slots__ = ()
917921
... @property
918922
... def hypot(self):
@@ -963,8 +967,10 @@ customize a prototype instance:
963967
constructor that is convenient for use cases where named tuples are being
964968
subclassed.
965969

966-
* :meth:`types.SimpleNamespace` for a mutable namespace based on an underlying
967-
dictionary instead of a tuple.
970+
* See :meth:`types.SimpleNamespace` for a mutable namespace based on an
971+
underlying dictionary instead of a tuple.
972+
973+
* See :meth:`typing.NamedTuple` for a way to add type hints for named tuples.
968974

969975

970976
:class:`OrderedDict` objects

0 commit comments

Comments
 (0)