Faster and simpler _replace() method

This commit is contained in:
Raymond Hettinger 2007-12-14 19:19:59 +00:00
parent adbda844d0
commit 07ae83f840
2 changed files with 2 additions and 2 deletions

View File

@ -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))

View File

@ -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: