Leveraging __slots__ for Better Performance in Python Classes

Konrad - Aug 25 - - Dev Community

Each time when we create a new class python stores every attribute in a dict attribute which is called a dynamic dictionary. This default behaviour seems to be convenient, because it is flexible, but when you're working with a large number of instances or memory usage matters then this overhead can be significant.

How do '__slots__' work?

How do 'slots' work?

Python basically uses a dictionary to store class attributes, but one of the alternatives is to use slots. By defining this name, we are telling Python to use a more static and compact structure which significantly reduces memory usage. Here's a basic example of how to use slots in a class.

import sys 

class WithoutSlots:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class WithSlots:
    __slots__ = ['x', 'y']

    def __init__(self, x, y):
        self.x = x
        self.y = y

obj1 = WithoutSlots(1, 2)
obj2 = WithSlots(1, 2)

print(sys.getsizeof(obj1.__dict__)) # 296
print(sys.getsizeof(obj2)) # 48
Enter fullscreen mode Exit fullscreen mode

As shown above 'WithoutSlots' uses much more memory compared to 'WithSlots'. Think about creating many instances of the class - Which approach would be the better choice?

Limitations

Limitations

slots may be useful tool, but comes with limitations:

  • No dynamic attributes: while defining slots in the class body we disable its default attribute (dict), so we cannot dynamically add new attributes to the instance after it's creation.
obj = WithSlots(1, 2)
obj.z = 3  # This will raise an AttributeError
Enter fullscreen mode Exit fullscreen mode

We can get around this by adding dict to the slot.

  • No multiple inheritance: every base class must contain slots defined, otherwise python will revert to using dictionary to store the instance attributes.

  • No default value: You need to explicitly initialise default values explicitly in the init method.

When to use it

When to use it

I've written down some best scenario examples where we can use slots:

  • When we have a lot of instances to create and memory usage is a concern.
  • When we need to optimise performance.
  • When you have attributes that are known and fixed.
  • When you work with large datasets.

Final thoughts

Final thoughts

This is how slots are used in Python: you can use them when you're certain you won't need any other attributes for your class and you’re working with a large number of instances. By defining slots, you tell Python to use a more efficient and compact structure for storing attributes, which helps save memory. This is especially handy when memory usage is a concern or when you need to optimize performance. Just remember that with slots, you can't add new attributes dynamically, so it's best used when your class attributes are fixed and well-defined.

. .
Terabox Video Player