Does the Python standard library offer anything similar to the custom replace function below? I can put this in my own *utils module, but I'd rather use a standard library implementation. Also, this example would better be served by a namedtuple, which already has _replace, but I need the same function for other classes in my project.
from copy import copy
class cab(object):
a = None
b = None
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return "cab(a=%s, b=%s)" % (self.a, self.b)
# Does not modify source. Creates a copy with the specified modified fields.
def replace(source, **kwargs):
result = copy(source)
for key in kwargs:
setattr(result, key, kwargs[key])
return result
v1 = cab(3,4)
v2 = replace(v1, a=100)
v3 = replace(v1, b=100)