Return the fields of a dataclass instance as a new tuple of field values. Example usage:: @dataclass class C: x: int y: int c = C(1, 2) assert astuple(c) == (1, 2) If given, 'tuple_factory' will be used instead of built-in tuple. The functi
(obj, *, tuple_factory=tuple)
| 1566 | |
| 1567 | |
| 1568 | def astuple(obj, *, tuple_factory=tuple): |
| 1569 | """Return the fields of a dataclass instance as a new tuple of field values. |
| 1570 | |
| 1571 | Example usage:: |
| 1572 | |
| 1573 | @dataclass |
| 1574 | class C: |
| 1575 | x: int |
| 1576 | y: int |
| 1577 | |
| 1578 | c = C(1, 2) |
| 1579 | assert astuple(c) == (1, 2) |
| 1580 | |
| 1581 | If given, 'tuple_factory' will be used instead of built-in tuple. |
| 1582 | The function applies recursively to field values that are |
| 1583 | dataclass instances. This will also look into built-in containers: |
| 1584 | tuples, lists, and dicts. Other objects are copied with 'copy.deepcopy()'. |
| 1585 | """ |
| 1586 | |
| 1587 | if not _is_dataclass_instance(obj): |
| 1588 | raise TypeError("astuple() should be called on dataclass instances") |
| 1589 | return _astuple_inner(obj, tuple_factory) |
| 1590 | |
| 1591 | |
| 1592 | def _astuple_inner(obj, tuple_factory): |
searching dependent graphs…