From e850c466c7dd25baac71997d6ca073e3586526b5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 10 Jan 2008 20:37:12 +0000 Subject: [PATCH] Clarify how to add a field to a named tuple. --- Doc/library/collections.rst | 2 +- Lib/collections.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index b276ab02d9c..1e213fb9cb7 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 47b0397d0b8..267c39f9e43 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())