04 / 05

What is the difference between __slots__ and a regular class's default __dict__-based attribute storage, and how does __slots__ improve memory efficiency?

Difficulty: 8/10
Slots, Memory Layout, Classes

slots stores attributes in fixed slots instead of a per-instance dict

By default, each instance of a class gets a dict, a hash table that maps attribute names to values. That is flexible but expensive: the dict itself has overhead, and every instance carries it even if the class has two attributes. Defining slots replaces the per-instance dict with a fixed set of C-level descriptors, one per slot, so an instance stores only the declared attributes in pre-allocated offsets. The savings are significant for classes created in large numbers: often 40 to 60 percent less memory per instance. The costs are real too: you cannot add arbitrary attributes, slots do not participate in dict-based tooling like vars(), and multiple inheritance with non-empty slots is restricted to one base with non-empty slots and the rest with empty slots. Choose slots for data-heavy value objects created in bulk, and avoid them when you need dynamic attributes or heavy introspection.

  1. 1

    Slots eliminate the per-instance dict, saving memory and slightly speeding up attribute access.

  2. 2

    Declare slots as a tuple of names to save a few bytes over a list.

  3. 3

    Include weakref and dict explicitly in slots if you need weak references or dynamic attributes.

  4. 4

    dataclasses support slots=True since 3.10, which is the cleanest way to get both.

  5. 5

    Trade-off: slots remove flexibility. Dynamic attributes, monkey-patching, and some serialization libraries break.

  6. 6

    Common mistake: defining slots in a subclass while the parent still has dict, which keeps the dict and negates most savings.

  7. 7

    Common mistake: assuming slots makes instances immutable. It does not; assignment to declared slots still works.

  8. 8

    Version note: dataclass slots=True arrived in 3.10. The basic slot semantics have been stable since Python 2.2.

Scenario Questions

0-2 years experience

  1. 1Why does a class with __slots__ use less memory per instance?
  2. 2Can you add a new attribute to an instance of a class with __slots__?

2-5 years experience

  1. 1You add __slots__ to a subclass but memory does not improve. Why?
  2. 2You need both slots and weak references on an instance. How do you declare it?

5-8 years experience

  1. 1You create millions of small records in a pipeline and memory is the bottleneck. How do you apply slots and measure the improvement?
  2. 2A serialization library breaks after you add slots. How do you reconcile serialization with slots?

8+ years experience

  1. 1Design a data model for high-volume ingestion that minimizes per-instance overhead while remaining introspectable and serializable.
  2. 2Compare __slots__, dataclasses with slots, and namedtuples on memory, attribute access speed, and compatibility with typing and pickling.

Follow-up Questions

  • What happens if a parent class has __dict__ and a child defines __slots__?
  • When would __slots__ make attribute access slower rather than faster?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.