mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
Clarify how to add a field to a named tuple.
This commit is contained in:
parent
e1655088ca
commit
e850c466c7
2 changed files with 4 additions and 1 deletions
|
|
@ -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::
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue