Question
When should a Python developer choose __slots__ deliberately?
- Choose __slots__ mainly when you want to postpone validation and fix issues manually later.
- Choose __slots__ whenever you want the code to look more advanced, even if the design gets less clear.
- Use __slots__ when you have many lightweight instances and a controlled attribute set that genuinely benefits from lower overhead.
- Choose __slots__ only to avoid modeling the real data shape or domain contract explicitly.
Hint
Think about the production scenario where the choice genuinely improves the code.
Answer and rationale
Correct answer: C. Use __slots__ when you have many lightweight instances and a controlled attribute set that genuinely benefits from lower overhead.
Use __slots__ when you have many lightweight instances and a controlled attribute set that genuinely benefits from lower overhead. Interviewers often ask this to see whether you can connect the concept to real design decisions.
Track: Python