Leveraging __slots__ for Better Performance in Python Classes

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…


This content originally appeared on DEV Community and was authored by Konrad

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

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

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.


This content originally appeared on DEV Community and was authored by Konrad


Print Share Comment Cite Upload Translate Updates
APA

Konrad | Sciencx (2024-08-25T19:06:41+00:00) Leveraging __slots__ for Better Performance in Python Classes. Retrieved from https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/

MLA
" » Leveraging __slots__ for Better Performance in Python Classes." Konrad | Sciencx - Sunday August 25, 2024, https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/
HARVARD
Konrad | Sciencx Sunday August 25, 2024 » Leveraging __slots__ for Better Performance in Python Classes., viewed ,<https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/>
VANCOUVER
Konrad | Sciencx - » Leveraging __slots__ for Better Performance in Python Classes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/
CHICAGO
" » Leveraging __slots__ for Better Performance in Python Classes." Konrad | Sciencx - Accessed . https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/
IEEE
" » Leveraging __slots__ for Better Performance in Python Classes." Konrad | Sciencx [Online]. Available: https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/. [Accessed: ]
rf:citation
» Leveraging __slots__ for Better Performance in Python Classes | Konrad | Sciencx | https://www.scien.cx/2024/08/25/leveraging-__slots__-for-better-performance-in-python-classes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.