`__slots__` Memory Magic: Fixed Array vs. Dynamic Dict

Defining `__slots__` replaces Python's default dynamic `__dict__` for attribute storage with a fixed-size array, eliminating per-instance dictionary overhead and enabling faster attribute access via direct memory offsets. This can lead to substantial memory savings, especially for numerous small objects, with reductions ranging from 25% to over 70% in some cases, though Python 3.11+ optimizations typically yield 32-64 bytes per instance.

Sources

Open the full topic