Simplify implementation of __replace__()

This commit is contained in:
Raymond Hettinger 2007-12-13 22:55:52 +00:00
parent e552959184
commit 04a9a0e904
2 changed files with 2 additions and 2 deletions

View File

@ -396,7 +396,7 @@ Example::
return dict(zip(('x', 'y'), self)) return dict(zip(('x', 'y'), self))
def __replace__(self, **kwds): def __replace__(self, **kwds):
'Return a new Point object replacing specified fields with new values' 'Return a new Point object replacing specified fields with new values'
return Point(**dict(zip(('x', 'y'), self) + kwds.items())) return Point(**dict(zip(('x', 'y'), self), **kwds))
x = property(itemgetter(0)) x = property(itemgetter(0))
y = property(itemgetter(1)) y = property(itemgetter(1))

View File

@ -70,7 +70,7 @@ def namedtuple(typename, field_names, verbose=False):
return dict(zip(%(field_names)r, self)) return dict(zip(%(field_names)r, self))
def __replace__(self, **kwds): def __replace__(self, **kwds):
'Return a new %(typename)s object replacing specified fields with new values' 'Return a new %(typename)s object replacing specified fields with new values'
return %(typename)s(**dict(zip(%(field_names)r, self) + kwds.items())) \n''' % locals() return %(typename)s(**dict(zip(%(field_names)r, self), **kwds)) \n''' % locals()
for i, name in enumerate(field_names): for i, name in enumerate(field_names):
template += ' %s = property(itemgetter(%d))\n' % (name, i) template += ' %s = property(itemgetter(%d))\n' % (name, i)
if verbose: if verbose: