diff options
author | Raymond Hettinger <python@rcn.com> | 2009-03-03 04:51:24 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-03-03 04:51:24 +0000 |
commit | 88a9164cdba48dba05b6ebe1076ba641b4a50ffa (patch) | |
tree | a9706c423672fc5425475ef4887fb86ddeca9cbc /Lib/collections.py | |
parent | bc512d3abd93e30ab452267647106c744fa4e870 (diff) | |
download | cpython-git-88a9164cdba48dba05b6ebe1076ba641b4a50ffa.tar.gz |
Backport 70106: Add OrderedDict support to collections.namedtuple().
Diffstat (limited to 'Lib/collections.py')
-rw-r--r-- | Lib/collections.py | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index e807a50ad7..351bf40f2b 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -164,7 +164,6 @@ def namedtuple(typename, field_names, verbose=False, rename=False): numfields = len(field_names) argtxt = repr(field_names).replace("'", "")[1:-1] # tuple repr without parens or quotes reprtxt = ', '.join('%s=%%r' % name for name in field_names) - dicttxt = ', '.join('%r: t[%d]' % (name, pos) for pos, name in enumerate(field_names)) template = '''class %(typename)s(tuple): '%(typename)s(%(argtxt)s)' \n __slots__ = () \n @@ -180,9 +179,9 @@ def namedtuple(typename, field_names, verbose=False, rename=False): return result \n def __repr__(self): return '%(typename)s(%(reprtxt)s)' %% self \n - def _asdict(t): - 'Return a new dict which maps field names to their values' - return {%(dicttxt)s} \n + def _asdict(self): + 'Return a new OrderedDict which maps field names to their values' + return OrderedDict(zip(self._fields, self)) \n def _replace(self, **kwds): 'Return a new %(typename)s object replacing specified fields with new values' result = self._make(map(kwds.pop, %(field_names)r, self)) @@ -198,7 +197,8 @@ def namedtuple(typename, field_names, verbose=False, rename=False): # Execute the template string in a temporary namespace and # support tracing utilities by setting a value for frame.f_globals['__name__'] - namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename) + namespace = dict(itemgetter=_itemgetter, __name__='namedtuple_%s' % typename, + OrderedDict=OrderedDict) try: exec template in namespace except SyntaxError, e: |