Backport 70106: Add OrderedDict support to collections.namedtuple().

This commit is contained in:
Raymond Hettinger 2009-03-03 04:51:24 +00:00
parent bc512d3abd
commit 88a9164cdb
3 changed files with 16 additions and 10 deletions

View file

@ -676,9 +676,9 @@ Example:
def __repr__(self):
return 'Point(x=%r, y=%r)' % self
<BLANKLINE>
def _asdict(t):
'Return a new dict which maps field names to their values'
return {'x': t[0], 'y': t[1]}
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'
@ -736,10 +736,14 @@ field names, the method and attribute names start with an underscore.
.. method:: somenamedtuple._asdict()
Return a new dict which maps field names to their corresponding values::
Return a new :class:`OrderedDict` which maps field names to their corresponding
values::
>>> p._asdict()
{'x': 11, 'y': 22}
OrderedDict([('x', 11), ('y', 22)])
.. versionchanged 3.1
Returns an :class:`OrderedDict` instead of a regular :class:`dict`.
.. method:: somenamedtuple._replace(kwargs)