summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-03-23 05:36:21 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-03-23 05:36:21 (GMT)
commitb2d0945c8783ab315520169c4bd4533ad82ad539 (patch)
tree4e78926247918273bb1a7bb9daec0ab3c933b9ce /Doc/library/collections.rst
parentfef854602bb86e396a1db82f91e9321eb16f4a97 (diff)
downloadcpython-b2d0945c8783ab315520169c4bd4533ad82ad539.zip
cpython-b2d0945c8783ab315520169c4bd4533ad82ad539.tar.gz
cpython-b2d0945c8783ab315520169c4bd4533ad82ad539.tar.bz2
Minor named tuple clean-ups.
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst65
1 files changed, 33 insertions, 32 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 2cd8c21..c893e22 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -711,47 +711,48 @@ they add the ability to access fields by name instead of position index.
>>> p = Point(x=10, y=11)
>>> # Example using the verbose option to print the class definition
- >>> Point = namedtuple('Point', 'x y', verbose=True)
+ >>> Point = namedtuple('Point', ['x', 'y'], verbose=True)
class Point(tuple):
- 'Point(x, y)'
+ 'Point(x, y)'
<BLANKLINE>
- __slots__ = ()
+ __slots__ = ()
<BLANKLINE>
- _fields = ('x', 'y')
+ _fields = ('x', 'y')
<BLANKLINE>
- def __new__(_cls, x, y):
- 'Create a new instance of Point(x, y)'
- return _tuple.__new__(_cls, (x, y))
+ def __new__(_cls, x, y):
+ 'Create a new instance of Point(x, y)'
+ return _tuple.__new__(_cls, (x, y))
<BLANKLINE>
- @classmethod
- def _make(cls, iterable, new=tuple.__new__, len=len):
- 'Make a new Point object from a sequence or iterable'
- result = new(cls, iterable)
- if len(result) != 2:
- raise TypeError('Expected 2 arguments, got %d' % len(result))
- return result
+ @classmethod
+ def _make(cls, iterable, new=tuple.__new__, len=len):
+ 'Make a new Point object from a sequence or iterable'
+ result = new(cls, iterable)
+ if len(result) != 2:
+ raise TypeError('Expected 2 arguments, got %d' % len(result))
+ return result
<BLANKLINE>
- def __repr__(self):
- 'Return a nicely formatted representation string'
- return self.__class__.__name__ + '(x=%r, y=%r)' % self
+ def __repr__(self):
+ 'Return a nicely formatted representation string'
+ return self.__class__.__name__ + '(x=%r, y=%r)' % self
<BLANKLINE>
- def _asdict(self):
- 'Return a new OrderedDict which maps field names to their values'
- return OrderedDict(zip(self._fields, self))
+ def _asdict(self):
+ 'Return a new OrderedDict which maps field names to their values'
+ return OrderedDict(zip(self._fields, self))
<BLANKLINE>
- def _replace(_self, **kwds):
- 'Return a new Point object replacing specified fields with new values'
- result = _self._make(map(kwds.pop, ('x', 'y'), _self))
- if kwds:
- raise ValueError('Got unexpected field names: %r' % list(kwds.keys()))
- return result
+ def _replace(_self, **kwds):
+ 'Return a new Point object replacing specified fields with new values'
+ result = _self._make(map(kwds.pop, ('x', 'y'), _self))
+ if kwds:
+ raise ValueError('Got unexpected field names: %r' % list(kwds))
+ return result
<BLANKLINE>
- def __getnewargs__(self):
- 'Return self as a plain tuple. Used by copy and pickle.'
- return tuple(self)
+ def __getnewargs__(self):
+ 'Return self as a plain tuple. Used by copy and pickle.'
+ return tuple(self)
<BLANKLINE>
- x = _property(_itemgetter(0), doc='Alias for field number 0')
- y = _property(_itemgetter(1), doc='Alias for field number 1')
+ x = _property(_itemgetter(0), doc='Alias for field number 0')
+ <BLANKLINE>
+ y = _property(_itemgetter(1), doc='Alias for field number 1')
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
@@ -867,7 +868,6 @@ a fixed-width print format:
The subclass shown above sets ``__slots__`` to an empty tuple. This helps
keep memory requirements low by preventing the creation of instance dictionaries.
-
Subclassing is not useful for adding new, stored fields. Instead, simply
create a new named tuple type from the :attr:`_fields` attribute:
@@ -879,6 +879,7 @@ customize a prototype instance:
>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> default_account = Account('<owner name>', 0.0, 0)
>>> johns_account = default_account._replace(owner='John')
+ >>> janes_account = default_account._replace(owner='Jane')
Enumerated constants can be implemented with named tuples, but it is simpler
and more efficient to use a simple class declaration: