diff options
author | Raymond Hettinger <python@rcn.com> | 2007-11-15 02:44:53 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2007-11-15 02:44:53 (GMT) |
commit | eeeb9c4445391f0ec91d84d5f35634ffe42c66de (patch) | |
tree | f912371af912aedc3469746cd3cd3e961ff33395 /Lib | |
parent | b5e5d0741a7a9b8219abac143e06f40b804886c9 (diff) | |
download | cpython-eeeb9c4445391f0ec91d84d5f35634ffe42c66de.zip cpython-eeeb9c4445391f0ec91d84d5f35634ffe42c66de.tar.gz cpython-eeeb9c4445391f0ec91d84d5f35634ffe42c66de.tar.bz2 |
Accept Issac Morland's suggestion for __replace__ to allow multiple replacements
(suprisingly, this simplifies the signature, improves clarity, and is comparably fast).
Update the docs to reflect a previous change to the function name.
Add an example to the docs showing how to override the default __repr__ method.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/collections.py | 12 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 2 |
2 files changed, 9 insertions, 5 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 40f5187..6b2e58d 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -24,7 +24,7 @@ def namedtuple(typename, field_names, verbose=False): 11 >>> Point(**d) # convert from a dictionary Point(x=11, y=22) - >>> p.__replace__('x', 100) # __replace__() is like str.replace() but targets a named field + >>> p.__replace__(x=100) # __replace__() is like str.replace() but targets named fields Point(x=100, y=22) """ @@ -62,9 +62,9 @@ def namedtuple(typename, field_names, verbose=False): def __asdict__(self, dict=dict, zip=zip): 'Return a new dict mapping field names to their values' return dict(zip(%(field_names)r, self)) - def __replace__(self, field, value, dict=dict, zip=zip): - 'Return a new %(typename)s object replacing one field with a new value' - return %(typename)s(**dict(zip(%(field_names)r, self) + [(field, value)])) \n''' % locals() + def __replace__(self, **kwds): + 'Return a new %(typename)s object replacing specified fields with new values' + return %(typename)s(**dict(self.__asdict__().items() + kwds.items())) \n''' % locals() for i, name in enumerate(field_names): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: @@ -98,6 +98,10 @@ if __name__ == '__main__': p = Point(x=10, y=20) assert p == loads(dumps(p)) + # test and demonstrate ability to override methods + Point.__repr__ = lambda self: 'Point(%.3f, %.3f)' % self + print p + import doctest TestResults = namedtuple('TestResults', 'failed attempted') print TestResults(*doctest.testmod()) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 04d4d9d..7c5b2dc 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -40,7 +40,7 @@ class TestNamedTuple(unittest.TestCase): self.assert_('__dict__' not in dir(p)) # verify instance has no dict self.assert_('__weakref__' not in dir(p)) self.assertEqual(p.__fields__, ('x', 'y')) # test __fields__ attribute - self.assertEqual(p.__replace__('x', 1), (1, 22)) # test __replace__ method + self.assertEqual(p.__replace__(x=1), (1, 22)) # test __replace__ method self.assertEqual(p.__asdict__(), dict(x=11, y=22)) # test __dict__ method # Verify that __fields__ is read-only |