diff options
author | Raymond Hettinger <python@rcn.com> | 2016-08-16 20:13:17 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2016-08-16 20:13:17 (GMT) |
commit | fd27f62a747603137d2577d7e36a795dbddb36b2 (patch) | |
tree | 729191bcca7a252d88a2204a55a43a0b1250fe85 /Doc/library/collections.rst | |
parent | f59249b048fa7abdc6667de3f777863bccb10a4c (diff) | |
download | cpython-fd27f62a747603137d2577d7e36a795dbddb36b2.zip cpython-fd27f62a747603137d2577d7e36a795dbddb36b2.tar.gz cpython-fd27f62a747603137d2577d7e36a795dbddb36b2.tar.bz2 |
Add cross-reference to typing.NamedTuple. Doctest two more examples.
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r-- | Doc/library/collections.rst | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 778bd54..6daee6f 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -850,7 +850,9 @@ field names, the method and attribute names start with an underscore. .. method:: somenamedtuple._asdict() Return a new :class:`OrderedDict` which maps field names to their corresponding - values:: + values: + + .. doctest:: >>> p = Point(x=11, y=22) >>> p._asdict() @@ -912,7 +914,9 @@ Since a named tuple is a regular Python class, it is easy to add or change functionality with a subclass. Here is how to add a calculated field and a fixed-width print format: - >>> class Point(namedtuple('Point', 'x y')): +.. doctest:: + + >>> class Point(namedtuple('Point', ['x', 'y'])): ... __slots__ = () ... @property ... def hypot(self): @@ -963,8 +967,10 @@ customize a prototype instance: constructor that is convenient for use cases where named tuples are being subclassed. - * :meth:`types.SimpleNamespace` for a mutable namespace based on an underlying - dictionary instead of a tuple. + * See :meth:`types.SimpleNamespace` for a mutable namespace based on an + underlying dictionary instead of a tuple. + + * See :meth:`typing.NamedTuple` for a way to add type hints for named tuples. :class:`OrderedDict` objects |