OBJECTSChapter 8 · Objects and Classes
Data classes
@dataclass writes the repetitive parts of a class for you: __init__, a readable __repr__ and __eq__, all generated from the field annotations. Give a field a default with =, and use field(default_factory=list) when each object needs its own fresh list.
Worked example
How it reads
- Each
name: typeline becomes a constructor parameter - The generated
__repr__prints every field - The generated
__eq__compares field by field
Common mistakes
tags: list = []is rejected: a shared list default would be the same list for every object. Usefield(default_factory=list).

Cloud tip: Add
frozen=True (@dataclass(frozen=True)) for objects that should never change after they are made.

