When do you use __slots__ instead of a per-instance __dict__?
PICTURE THIS: PYTHON TYPES
Simple meaning
__slots__ declares a fixed set of attributes and typically omits __dict__, saving memory when you allocate millions of objects.
WHY — Data Types instead of guessing?
Why interviewers care about Data Types:
on Data Types.
the situation, the default choice, and one exception - that reads as experience.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1__slots__ declares a fixed
set of attributes and typically omits __dict__, saving memory when you allocate millions of objects.
- 2The cost is less
dynamism: you cannot add arbitrary attributes unless you include '__dict__' in slots.
- 3Django models rely on
descriptors and __dict__
- 4Give an example
do not slot them casually.
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
__slots__ declares a fixed set of attributes and typically omits __dict__, saving memory when you allocate millions of objects. The cost is less dynamism: you cannot add arbitrary attributes unless you include '__dict__' in slots.