From: Raymond Hettinger Date: Thu, 10 Jan 2008 20:37:12 +0000 (+0000) Subject: Clarify how to add a field to a named tuple. X-Git-Tag: v2.6a1~654 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e850c466c7dd25baac71997d6ca073e3586526b5;p=thirdparty%2FPython%2Fcpython.git Clarify how to add a field to a named tuple. --- diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index b276ab02d9ce..1e213fb9cb75 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 47b0397d0b89..267c39f9e43d 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())