What is the main effect of defining __slots__ = ("x", "y") in a class?
Options
- It makes instances immutable, so no attribute can be reassigned
- It drops the per-instance
__dict__, saving memory and fixing the attributes (Correct option) - It makes the class thread-safe by locking every attribute access
- It turns the class into a dataclass with generated methods
Answer
Removes the per-instance __dict__, saving memory and blocking new attributes
Why
Normally each object keeps its attributes in its own dictionary (__dict__), which costs memory.
With __slots__, Python reserves fixed spaces only for the listed names and removes that dictionary.
Result: less memory per object (useful when you have millions of them), and you cannot add other attributes by mistake.
Remember
__slots__ = less memory and a fixed set of attributes.