Follow-up · depth 7
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.
Audio N/AQuick interview answer
Explain dataclasses and __slots__ in one sentence, then a tiny example, then one “don’t do this” tip. Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
Detailed answer
Junior-friendly explanation of dataclasses and __slots__: Plain English: Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal).. 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__
Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
View full parent question →