icon: package
@dataclass uses annotated fields to supply __init__, __repr__,
__eq__, and __ne__ when the class does not define them.
from dataclasses import dataclass, asdict
@dataclass
class Player:
name: str
score: int = 0
player = Player('Ada', score=10)
assert player == Player('Ada', 10)
assert asdict(player) == {'name': 'Ada', 'score': 10}
assert repr(player) == "Player(name='Ada', score=10)"
Fields without defaults must precede fields with defaults. Base-class annotations are included. Annotations describe fields; they do not validate the values assigned to them.
@dataclass directly. Decorator options such as frozen, order,
slots, and kw_only are not implemented.field(), default_factory, or automatic __post_init__()
call.__init__ instead.asdict(obj) produces a shallow field dictionary. Nested objects and
containers are not recursively converted or copied.Implementation: dataclasses.py.