summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-01-10 20:37:12 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-01-10 20:37:12 (GMT)
commite850c466c7dd25baac71997d6ca073e3586526b5 (patch)
tree523f765bb66dd223d20e10cef3708b5a5fafe279
parente1655088ca65644312f02a147a0f9ad968cde2d9 (diff)
downloadcpython-e850c466c7dd25baac71997d6ca073e3586526b5.zip
cpython-e850c466c7dd25baac71997d6ca073e3586526b5.tar.gz
cpython-e850c466c7dd25baac71997d6ca073e3586526b5.tar.bz2
Clarify how to add a field to a named tuple.
-rw-r--r--Doc/library/collections.rst2
-rw-r--r--Lib/collections.py3
2 files changed, 4 insertions, 1 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index b276ab0..1e213fb 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -538,7 +538,7 @@ faster versions that bypass error-checking and that localize variable access::
Subclassing is not useful for adding new, stored fields. Instead, simply
create a new named tuple type from the :attr:`_fields` attribute::
- >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
+ >>> Point3D = namedtuple('Point3D', Point._fields + ('z',))
Default values can be implemented by using :meth:`_replace` to
customize a prototype instance::
diff --git a/Lib/collections.py b/Lib/collections.py
index 47b0397..267c39f 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -137,6 +137,9 @@ if __name__ == '__main__':
print Point(11, 22)._replace(x=100)
+ Point3D = namedtuple('Point3D', Point._fields + ('z',))
+ print Point3D.__doc__
+
import doctest
TestResults = namedtuple('TestResults', 'failed attempted')
print TestResults(*doctest.testmod())