Clarify how to add a field to a named tuple.

This commit is contained in:
Raymond Hettinger 2008-01-10 20:37:12 +00:00
parent e1655088ca
commit e850c466c7
2 changed files with 4 additions and 1 deletions

View File

@ -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 Subclassing is not useful for adding new, stored fields. Instead, simply
create a new named tuple type from the :attr:`_fields` attribute:: 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 Default values can be implemented by using :meth:`_replace` to
customize a prototype instance:: customize a prototype instance::

View File

@ -137,6 +137,9 @@ if __name__ == '__main__':
print Point(11, 22)._replace(x=100) print Point(11, 22)._replace(x=100)
Point3D = namedtuple('Point3D', Point._fields + ('z',))
print Point3D.__doc__
import doctest import doctest
TestResults = namedtuple('TestResults', 'failed attempted') TestResults = namedtuple('TestResults', 'failed attempted')
print TestResults(*doctest.testmod()) print TestResults(*doctest.testmod())