Repaso
EspaƱol
PythonAdvancedPY-10

What is the main effect of defining __slots__ = ("x", "y") in a class?

Options

  1. It makes instances immutable, so no attribute can be reassigned
  2. It drops the per-instance __dict__, saving memory and fixing the attributes (Correct option)
  3. It makes the class thread-safe by locking every attribute access
  4. 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.

Key terms

Practice this question in Repaso