ID EN
Dataclasses

astuple

Python 3.11

Converts the dataclass obj to a tuple (by using the factory function tuple_factory). Each dataclass is converted to a tuple of its field values. dataclasses, dicts, lists, and tuples are recursed into. Other objects are copied with copy.deepcopy().

Syntax

PYTHON
dataclasses.astuple(obj, *, tuple_factory=tuple)

Examples

Example 1
PYTHON
assert astuple(p) == (10, 20)
assert astuple(c) == ([(0, 0), (10, 4)],)
Example 2
PYTHON
tuple(getattr(obj, field.name) for field in dataclasses.fields(obj))

See Also

copy.deepcopy() TypeError