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.
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?
slots may be useful tool, but comes with limitations:
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.
I've written down some best scenario examples where we can use slots:
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!