diff options
author | Raymond Hettinger <python@rcn.com> | 2007-09-18 03:33:19 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-09-18 03:33:19 (GMT) |
commit | 2b03d45bb9a26645dc06c9adfdeb5ec0b7509511 (patch) | |
tree | 020e496169fcf0338eebffc61306b732485a3677 /Doc/library/collections.rst | |
parent | de37a8cec7e9410477e93ae750c513cbc89955e0 (diff) | |
download | cpython-2b03d45bb9a26645dc06c9adfdeb5ec0b7509511.zip cpython-2b03d45bb9a26645dc06c9adfdeb5ec0b7509511.tar.gz cpython-2b03d45bb9a26645dc06c9adfdeb5ec0b7509511.tar.bz2 |
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r-- | Doc/library/collections.rst | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 7b639b3..2986071 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -364,7 +364,7 @@ Setting the :attr:`default_factory` to :class:`set` makes the -------------------------------------------- -.. function:: NamedTuple(typename, fieldnames) +.. function:: NamedTuple(typename, fieldnames, [verbose]) Returns a new tuple subclass named *typename*. The new subclass is used to create tuple-like objects that have fields accessable by attribute lookup as @@ -412,6 +412,23 @@ Setting the :attr:`default_factory` to :class:`set` makes the >>> print Color(*m.popitem()) Color(name='blue', code=3) + If *verbose* is true, the *NamedTuple* call will print the class definition:: + + >>> Point = NamedTuple('Point', 'x y', verbose=True) + class Point(tuple): + 'Point(x, y)' + __slots__ = () + __fields__ = ('x', 'y') + def __new__(cls, x, y): + return tuple.__new__(cls, (x, y)) + def __repr__(self): + return 'Point(x=%r, y=%r)' % self + def __replace__(self, field, value): + 'Return a new Point object replacing one field with a new value' + return Point(**dict(zip(('x', 'y'), self) + [(field, value)])) + x = property(itemgetter(0)) + y = property(itemgetter(1)) + In addition to the methods inherited from tuples, named tuples support an additonal method and an informational read-only attribute. |