Home  >  Article  >  Backend Development  >  Leveraging __slots__ for Better Performance in Python Classes

Leveraging __slots__ for Better Performance in Python Classes

王林
王林Original
2024-08-27 06:01:36684browse

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.

The above is the detailed content of Leveraging __slots__ for Better Performance in Python Classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn