Follow-up · depth 6
How would you explain dataclasses and __slots__ to a junior engineer?
How would you explain dataclasses and __slots__ to a junior engineer?
Answers use simple, clear English.
Quick interview answer
Explain dataclasses and __slots__ in one sentence, then a tiny example, then one “don’t do this” tip. Baseline: @dataclass auto-generates __init__, __repr__, __eq__. slots=True (Py3.10+) fixes attributes and saves memory.
Detailed answer
Junior-friendly explanation of dataclasses and __slots__: Plain English: Baseline: @dataclass auto-generates __init__, __repr__, __eq__. slots=True (Py3.10+) fixes attributes and saves memory. Tiny example: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal). One warning: Mutable default list in dataclass field; forgetting frozen for dict keys.
Full explanation
Keep jargon low, then layer detail. @dataclass auto-generates __init__, __repr__, __eq__. slots=True (Py3.10+) fixes attributes and saves memory. frozen=True makes instances hashable if all fields hashable.
Follow-up questions
Only answered follow-ups are shown — click to open with full answers
Parent context — dataclasses and __slots__
Baseline: @dataclass auto-generates __init__, __repr__, __eq__. slots=True (Py3.10+) fixes attributes and saves memory.
View full parent question →