summaryrefslogtreecommitdiffstats
path: root/Lib/collections
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2013-05-17 23:43:14 (GMT)
committerRaymond Hettinger <python@rcn.com>2013-05-17 23:43:14 (GMT)
commit587d3bf78a091c9629fc7952e47197838d338d47 (patch)
tree0c4cc8071f19ef04c6a36e4429ccfd95cc6b2758 /Lib/collections
parentd2b58a9880d119b20e1fc1c82a3e2e2d9eaa0817 (diff)
downloadcpython-587d3bf78a091c9629fc7952e47197838d338d47.zip
cpython-587d3bf78a091c9629fc7952e47197838d338d47.tar.gz
cpython-587d3bf78a091c9629fc7952e47197838d338d47.tar.bz2
Update docstring for _asdict() to indicate it is obsolete.
Use the cleaner looking @property style for __dict__. Move _replace() to be just after make() to indicate that it is a core method on named tuples.
Diffstat (limited to 'Lib/collections')
-rw-r--r--Lib/collections/__init__.py29
1 files changed, 17 insertions, 12 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 5ba29e6..254409b 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -236,7 +236,7 @@ class OrderedDict(dict):
### namedtuple
################################################################################
-_class_template = '''\
+_class_template = """\
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
@@ -260,16 +260,6 @@ class {typename}(tuple):
raise TypeError('Expected {num_fields:d} arguments, got %d' % len(result))
return result
- def __repr__(self):
- 'Return a nicely formatted representation string'
- return self.__class__.__name__ + '({repr_fmt})' % self
-
- def _asdict(self):
- 'Return a new OrderedDict which maps field names to their values'
- return OrderedDict(zip(self._fields, self))
-
- __dict__ = property(_asdict)
-
def _replace(_self, **kwds):
'Return a new {typename} object replacing specified fields with new values'
result = _self._make(map(kwds.pop, {field_names!r}, _self))
@@ -277,6 +267,21 @@ class {typename}(tuple):
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
+ def __repr__(self):
+ 'Return a nicely formatted representation string'
+ return self.__class__.__name__ + '({repr_fmt})' % self
+
+ @property
+ def __dict__(self):
+ 'A new OrderedDict mapping 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.
+ This method is obsolete. Use vars(nt) or nt.__dict__ instead.
+ '''
+ return self.__dict__
+
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
@@ -286,7 +291,7 @@ class {typename}(tuple):
return None
{field_defs}
-'''
+"""
_repr_template = '{name}=%r'