From 07ae83f8400702a78825204751604b98b6f69c65 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 14 Dec 2007 19:19:59 +0000 Subject: [PATCH] Faster and simpler _replace() method --- Doc/library/collections.rst | 2 +- Lib/collections.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index d5f4283f8a9..d782c6927d5 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -402,7 +402,7 @@ Example:: def _replace(self, **kwds): 'Return a new Point object replacing specified fields with new values' - return Point(**dict(zip(('x', 'y'), self), **kwds)) + return Point(*map(kwds.get, ('x', 'y'), self)) x = property(itemgetter(0)) y = property(itemgetter(1)) diff --git a/Lib/collections.py b/Lib/collections.py index c3173db9da3..be4d4fdc6f8 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -71,7 +71,7 @@ def namedtuple(typename, field_names, verbose=False): return dict(zip(%(field_names)r, self)) \n def _replace(self, **kwds): 'Return a new %(typename)s object replacing specified fields with new values' - return %(typename)s(**dict(zip(%(field_names)r, self), **kwds)) \n\n''' % locals() + return %(typename)s(*map(kwds.get, %(field_names)r, self)) \n\n''' % locals() for i, name in enumerate(field_names): template += ' %s = property(itemgetter(%d))\n' % (name, i) if verbose: