"""Return the fields of a dataclass instance as a new dictionary mapping
field names to field values.
- Example usage:
+ Example usage::
@dataclass
class C:
x: int
y: int
- c = C(1, 2)
- assert astuple(c) == (1, 2)
+ c = C(1, 2)
+ assert astuple(c) == (1, 2)
If given, 'tuple_factory' will be used instead of built-in tuple.
The function applies recursively to field values that are
The dataclass name will be 'cls_name'. 'fields' is an iterable
of either (name), (name, type) or (name, type, Field) objects. If type is
omitted, use the string 'typing.Any'. Field objects are created by
- the equivalent of calling 'field(name, type [, Field-info])'.
+ the equivalent of calling 'field(name, type [, Field-info])'.::
C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))
- is equivalent to:
+ is equivalent to::
@dataclass
class C(Base):
def replace(obj, /, **changes):
"""Return a new object replacing specified fields with new values.
- This is especially useful for frozen classes. Example usage:
+ This is especially useful for frozen classes. Example usage::
@dataclass(frozen=True)
class C:
c = C(1, 2)
c1 = replace(c, x=3)
assert c1.x == 3 and c1.y == 2
- """
+ """
# We're going to mutate 'changes', but that's okay because it's a
# new dict, even if called with 'replace(obj, **my_changes)'.