diff options
author | Raymond Hettinger <python@rcn.com> | 2011-06-03 03:40:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-06-03 03:40:35 (GMT) |
commit | 45b082935d140bff4c6ddc17304be4bf9164a99a (patch) | |
tree | f96d171258b6516930f7b9240559105161323b7b | |
parent | 19b851d11b04d1490040ff296ebec57dc3d7eecb (diff) | |
download | cpython-45b082935d140bff4c6ddc17304be4bf9164a99a.zip cpython-45b082935d140bff4c6ddc17304be4bf9164a99a.tar.gz cpython-45b082935d140bff4c6ddc17304be4bf9164a99a.tar.bz2 |
Fix named tuples to work with vars().
-rw-r--r-- | Doc/library/collections.rst | 4 | ||||
-rw-r--r-- | Lib/collections.py | 1 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 2 | ||||
-rw-r--r-- | Misc/NEWS | 2 |
4 files changed, 7 insertions, 2 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 53e5ff9..0f098f7 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -623,7 +623,9 @@ Example: 'Return a new OrderedDict which maps field names to their values' return OrderedDict(zip(self._fields, self)) <BLANKLINE> - def _replace(_self, **kwds): + __dict__ = property(_asdict) + <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: diff --git a/Lib/collections.py b/Lib/collections.py index d87c55d..958e523 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -312,6 +312,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False): def _asdict(self): 'Return a new OrderedDict which maps field names to their values' return OrderedDict(zip(self._fields, self)) \n + __dict__ = property(_asdict) \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)) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 8bdeb3d..313f81f 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -78,12 +78,12 @@ class TestNamedTuple(unittest.TestCase): self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument self.assertEqual(repr(p), 'Point(x=11, y=22)') - self.assertNotIn('__dict__', dir(p)) # verify instance has no dict self.assertNotIn('__weakref__', dir(p)) self.assertEqual(p, Point._make([11, 22])) # test _make classmethod self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method + self.assertEqual(vars(p), p._asdict()) # verify that vars() works try: p._replace(x=1, error=2) @@ -16,6 +16,8 @@ Core and Builtins Library ------- +- Named tuples now work correctly with vars(). + - sys.setcheckinterval() now updates the current ticker count as well as updating the check interval, so if the user decreases the check interval, the ticker doesn't have to wind down to zero from the old starting point before the new |