In Python, class attributes are usually stored in a dictionary called __dict__
. This allows for dynamic addition of attributes but can be memory-inefficient. Python offers an alternative: the __slots__
attribute. By defining __slots__
in a class, you specify a fixed set of attributes, eliminating the need for a __dict__
.
How it Works
Default Storage: Normally, attributes are stored in a
__dict__
dictionary for each instance.Using
__slots__
: To restrict attributes, define a__slots__
tuple in the class.class Point: __slots__ = ('x', 'y', 'z') def __init__(self, x, y, z): self.x, self.y, self.z = x, y, z
- This prevents the addition of new attributes.
- Eliminates the
__dict__
, thus saving memory.
Why Use __slots__
- Memory Efficiency: Each instance uses a more efficient data structure instead of a dictionary.
- Faster Attribute Lookup: Avoids the computational cost of dictionary key lookups.
Practical Example
In a program that processes a large file to create a million instances of a Point
class, using __slots__
reduced memory usage from 271,660 bytes to 176,328 bytes.
When to Use
Use __slots__
when you have thousands of instances or require frequent attribute lookups. However, it restricts dynamic attribute addition, so use it judiciously.