- Daily Dose of Data Science
- Posts
- An Underrated Technique to Create Robust and Memory Efficient Class Objects
An Underrated Technique to Create Robust and Memory Efficient Class Objects
Fix the attributes an object can ever possess.
When we define a class in Python, it is possible to dynamically add new attributes to its objects during run-time.
For instance, consider the following Python class:
Now, it is perfectly legal to add new attributes to any object at run-time, as shown below:
However, this is not always recommended because:
It may lead to bugs if the code assumes all class instances will always have the same attributes.
It makes it difficult to debug code when objects keep on accumulating new attributes.
It leads to a conflicting schema, etc.
Can we restrict this dynamicity?
Of course we can!
Defining a slotted class helps us achieve this.
Simply put, it allows us to fix the instance-level attributes a class object can ever possess.
A slotted class is declared as follows:
Define a __slots__ attribute with all attributes an object may possess.
Done!
Now, if we try to add a new attribute at run-time, it raises an error:
Declaring a slotted class has many advantages as well.
For instance, it can help us avoid typical code typos.
Say we want to change the name of studentA.
Here, instead of referring to the attribute name (with a lowercase n), we mistakenly wrote Name (with an uppercase n).
A normal unslloted class will not raise an error, as shown below:
But a slotted class will catch this typo:
Declaring a slotted class provides memory advantages as well.
As shown below, an object of the slotted class consumes 2.5x less memory than an object of a normal class:
Why, you may wonder?
Typically, Python creates a dictionary for every object that maps attributes to their values:
As a dictionary is mutable, this is precisely what allows us to add/delete attributes dynamically.
But this introduces memory overheads as Python always tries to keep room for new attributes that may get added at some point.
In case of slotted class, however, Python can do away with this dictionary:
As a result, the object consumes less memory.
Isn’t that cool?
As a departing note, if you don’t want to dynamically add new attributes to an object, it is better to create a slotted class.
In fact, even if you know the attributes that a class object will ever possess, but some of these attributes are not available during the object’s initialization, you can still declare them in the __slots__ class attribute and assign a value to them later in the program whenever they are available.
👉 Over to you: While slotted classes do provide many benefits, when would you not prefer using them?
👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.
Thanks for reading!
Latest full articles
If you’re not a full subscriber, here’s what you missed last month:
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!
Reply