"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Leveraging __slots__ for Better Performance in Python Classes

Leveraging __slots__ for Better Performance in Python Classes

Published on 2024-11-12
Browse:540

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.

Leveraging __slots__ for Better Performance in Python Classes

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

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?

Leveraging __slots__ for Better Performance in Python Classes

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

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.

Leveraging __slots__ for Better Performance in Python Classes

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.

Leveraging __slots__ for Better Performance in Python Classes

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.

Release Statement This article is reproduced at: https://dev.to/conradylx/leveraging-slots-for-better-performance-in-python-classes-2ol4?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3