I'd like to replace the attributes of a dataclass instance, analogous to namedtuple._replace()
, i.e. making an altered copy of the original object:
from dataclasses import dataclass
from collections import namedtuple
U = namedtuple("U", "x")
@dataclass
class V:
x: int
u = U(x=1)
u_ = u._replace(x=-1)
v = V(x=1)
print(u)
print(u_)
print(v)
This returns:
U(x=1)
U(x=-1)
V(x=1)
How can I mimic this functionality in dataclass objects?