Python/Classes & structure 8 min

Dataclasses and type hints

Records without the boilerplate.

dataclasstype hints

@dataclass writes __init__, __repr__ and __eq__ for you from annotated fields — ideal for the record-shaped objects that fill data code.

python · editable
loading editor…

Try it: Add a units field defaulting to 1 and use it in a revenue property.

Mutable defaults need field(default_factory=list) — sharing one list across every instance is a notorious bug.

Type hints do not change runtime behaviour, but they document intent and let editors and type checkers catch mistakes early.

python · editable
loading editor…

practice this