dataclasses.replace()
dataclasses.replace() is a Python function in the Dataclasses Module category. Creates a new dataclass instance with specified fields changed (like copy with changes). The syntax is dataclasses.replace(obj, /, **changes). Parameters: obj, **changes. Returns: New dataclass instance. A typical example: p2 = dataclasses.replace(p, age=31)
# Person(name="Alice", age=31). A close sibling is dataclasses.dataclass(), which a decorator that generates __init__, __repr__, __eq__, and other methods for classes. A close sibling is dataclasses.field(), which customizes a single data class field (default factory, exclude from repr, etc.). A close sibling is dataclasses.asdict(), which converts a dataclass instance to a dictionary (recursively). A close sibling is dataclasses.astuple(), which converts a dataclass instance to a tuple. More about this category: Data classes — dataclass, field, asdict, replace, is_dataclass. Related Dataclasses Module entries: dataclasses.dataclass(), dataclasses.field(), dataclasses.asdict(), dataclasses.astuple(), dataclasses.is_dataclass(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.