Create Robust and Memory Efficient Class Objects
Fix the attributes an object can ever possess.
Before I begin…
I once promised to write an AI review article that will give a retrospective look at some of the most pivotal developments in AI over the last decade or so.
But it kept getting delayed.
I finally made some time to put it together and recently published it in AIport (a newsletter dedicated to AI developments to which I am contributing). Here’s the visual:
I have published them in two parts (both available for free):
12 Years of AI Review — Part 1: Covers pivotal AI moments from 2012-2017.
12 Years of AI Review — Part 2: Covers pivotal AI moments from 2018-2023.
I am pretty sure this will help you develop an in-depth understanding of how we got to where we are today.
Let’s get to today’s post now!
When we define a class in Python, it is possible to dynamically add new attributes to its objects during run-time.
On a side note, this dynamicity is a major contributor to the slowness of Python. But it can be restricted. We covered it in a recent issue here: Cython: An Under-appreciated Technique to Speed-up Native Python Programs.
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.
Here’s a full deep dive into Python OOP if you want to learn more about advanced OOP in Python: Object-Oriented Programming with Python for Data Scientists.
👉 Over to you: While slotted classes do provide many benefits, when would you not prefer using them?
Thanks for reading!
Whenever you are ready, here’s one more way I can help you:
Every week, I publish 1-2 in-depth deep dives (typically 20+ mins long). Here are some of the latest ones that you will surely like:
[FREE] A Beginner-friendly and Comprehensive Deep Dive on Vector Databases.
A Detailed and Beginner-Friendly Introduction to PyTorch Lightning: The Supercharged PyTorch
You Are Probably Building Inconsistent Classification Models Without Even Realizing
Why Sklearn’s Logistic Regression Has no Learning Rate Hyperparameter?
PyTorch Models Are Not Deployment-Friendly! Supercharge Them With TorchScript.
Federated Learning: A Critical Step Towards Privacy-Preserving Machine Learning.
You Cannot Build Large Data Projects Until You Learn Data Version Control!
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 If you love reading this newsletter, feel free to share it with friends!
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
Awesome
How did you create beautiful diagram? What tool you used for it