MCPcopy Index your code
hub / github.com/RustPython/RustPython / asdict

Function asdict

Lib/dataclasses.py:1475–1496  ·  view source on GitHub ↗

Return the fields of a dataclass instance as a new dictionary mapping field names to field values. Example usage:: @dataclass class C: x: int y: int c = C(1, 2) assert asdict(c) == {'x': 1, 'y': 2} If given, 'dict_factory' will be used inst

(obj, *, dict_factory=dict)

Source from the content-addressed store, hash-verified

1473
1474
1475def asdict(obj, *, dict_factory=dict):
1476 """Return the fields of a dataclass instance as a new dictionary mapping
1477 field names to field values.
1478
1479 Example usage::
1480
1481 @dataclass
1482 class C:
1483 x: int
1484 y: int
1485
1486 c = C(1, 2)
1487 assert asdict(c) == {'x': 1, 'y': 2}
1488
1489 If given, 'dict_factory' will be used instead of built-in dict.
1490 The function applies recursively to field values that are
1491 dataclass instances. This will also look into built-in containers:
1492 tuples, lists, and dicts. Other objects are copied with 'copy.deepcopy()'.
1493 """
1494 if not _is_dataclass_instance(obj):
1495 raise TypeError("asdict() should be called on dataclass instances")
1496 return _asdict_inner(obj, dict_factory)
1497
1498
1499def _asdict_inner(obj, dict_factory):

Calls 2

_is_dataclass_instanceFunction · 0.85
_asdict_innerFunction · 0.85