31.6 Slot Descriptors and __slots__
While descriptors provide a powerful mechanism for managing attribute access, Python classes inherently rely on a dictionary (__dict__) to store instance attributes. This offers tremendous flexibility but comes with a memory cost: every instance must allocate a dictionary, which can be significant when creating many small objects. The __slots__ class variable offers a high-performance alternative by fundamentally changing how instances store their data. How slots Replaces dict When you define __slots__ in a class, you are instructing the Python interpreter to create a fixed set of names (slots) for attributes on each instance. Instead of a dynamic dictionary, the interpreter reserves space for a small, fixed-length array (a C-style struct) within each instance to hold the values for these predefined attributes. This transformation happens deep within the object creation machinery.