]> granicus.if.org Git - python/commitdiff
Fix named tuples to work with vars().
authorRaymond Hettinger <python@rcn.com>
Fri, 3 Jun 2011 03:40:35 +0000 (20:40 -0700)
committerRaymond Hettinger <python@rcn.com>
Fri, 3 Jun 2011 03:40:35 +0000 (20:40 -0700)
Doc/library/collections.rst
Lib/collections.py
Lib/test/test_collections.py
Misc/NEWS

index 53e5ff96f8dbf0ef8c8c17b45f3db8275200664e..0f098f716e88e0ca48c32dc2565410e74e7d2fae 100644 (file)
@@ -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:
index d87c55d36b859b9decc18877972336ae29bdb2c5..958e523b6c701dbc3c2a9889424175bc2127d412 100644 (file)
@@ -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))
index 8bdeb3d8f1f9b883fa5597fe084a29cc37ee4f82..313f81ff65670b3ed7fe51bc1b902cb172b6fe99 100644 (file)
@@ -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)
index 34d66ec87777b6eb245fef72df208e9adfdcd356..faca1ac505d14de4b8b0cffd748d6acb30b7c873 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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