Follow-up · depth 6
What trade-offs come with dataclasses and __slots__?
What trade-offs come with dataclasses and __slots__?
Answers use simple, clear English.
Quick interview answer
Pros: Less boilerplate than manual classes; slots reduce per-instance dict overhead. Cons: Inheritance with slots tricky; default mutable fields need default_factory.
Detailed answer
Trade-offs for dataclasses and __slots__: Pros: Less boilerplate than manual classes; slots reduce per-instance dict overhead. Cons: Inheritance with slots tricky; default mutable fields need default_factory. Decision rule: choose dataclasses and __slots__ when the benefits outweigh operational cost for your SLO. Parent context: Use dataclasses and __slots__ as the core idea. Example shape: DTO layer: @dataclass(slots=True) OrderItem(sku: str, qty: int, price: Decimal)..
Full explanation
Strong answers state both sides and when you would pick an alternative. Pros: Less boilerplate than manual classes; slots reduce per-instance dict overhead. Cons: Inheritance with slots tricky; default mutable fields need default_factory.
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 →