What is the purpose of __slots__ in a Python class?

Python Developer Medium

Python Developer — Medium

What is the purpose of __slots__ in a Python class?

Key points

  • __slots__ restricts instance attributes, reducing memory usage.
  • It eliminates the per-instance __dict__.
  • Helps in defining a fixed set of attributes for instances.

Ready to go further?

Related questions