Python Glossary Русский

Python 3.7+dataclassesfunction

dataclasses.astuple

conversion

Recursively convert a dataclass instance into a tuple of field values.

Syntax

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

A tuple drops the field names, so any code unpacking it breaks silently the moment fields are reordered or inserted — for serialization and storage asdict is the safer choice. Like asdict it recurses and deep-copies, so a nested dataclass becomes a nested tuple rather than a reference to the original object. Reach for it when you genuinely want a short positional bundle: unpacking, a sort key, or splatting into a call.

Examples

import dataclasses
@dataclasses.dataclass
class P:
    x: int
    y: int
print(dataclasses.astuple(P(1, 2)))   # → (1, 2)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy