9221 of 11673 Respondents Answered This Poll Incorrectly
What happens when you instantiate a new object?
This is a poll I asked on LinkedIn 6 months back:
What really surprised me here was that most Python programmers hugely misinterpret the __init__()
in Python OOP.
They think that it is the first method that is executed when we create a new object, i.e., one that allocates memory to it.
But this is completely wrong.
To understand this, consider the Point2D class below:
When we create an object (shown below), programmers believe that in the background, it is the __init__()
method that is allocating memory to their object:
But that is not true.
When we create ANY object in Python, the __init__()
method is NEVER called first, and it NEVER allocates memory to it.
As the name suggests, __init__()
only assigns value to an object’s attributes, i.e., initialize the attributes.
Instead, it’s the __new__()
magic method that creates a new object and allocates memory to it.
To understand better, consider the class implementation below.
Here, we have implemented the __new__()
method, which checks if the passed arguments are of integer type.
Now, if we try to create an object of this class, Python would first validate the checks specified in the __new__ ()
method and create a new object only when the specified conditions are true.
This is evident from the image below:
Another popular use case of the __new__
method is to define singleton classes — classes that can only have one object.
For instance, consider the following class implementation:
In the above code, the __new__
method define a class variable _class_count
.
When a new object is instantiated, if the value of _class_count=0
, the value of _class_count
is updated to 1
and the object is returned.
After creating the first object, a new object can never be created because the value of _class_count
will never be 0
.
This is evident from the image below:
The above explanation shows that __init__()
is not the first method that is invoked when we create a new object.
The internet is full of shallow resources that leave several knowledge gaps in the learner’s mind (which also explains the poll results above).
This newsletter and the exclusive community are my attempts to fix this by building a reliable repository of core DS/ML techniques/concepts/ideas that enthusiasts and practitioners can genuinely learn from and have as few knowledge gaps as possible.
If you feel crushed by the shallowness, the exclusive community can be an excellent fit for you.
Every week, I write:
practical (so there are always implementations)
and in-depth (so every article is 15-20-25 minutes long)
…deep dives covering almost every little detail I could think of on topics that truly matter to your skills for succeeding and staying relevant in ML & DS roles.
So when you apply for ML/DS roles, you don’t get dismissed as just another generic applicant; when you’re interviewed, your responses don’t reveal gaps in core ML expertise; and when you take on real-world projects, you don’t feel clueless about how to approach problems.
If you want to build a career on core expertise, not fleeting trends, come join us here.
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.
Also, lately, I have been finding Python Descriptors to be massively helpful in reducing work and code redundancy while also making the entire implementation much more elegant.
We covered it in this newsletter issue: Define Elegant and Concise Python Classes with Descriptors.
👉 Over to you: What are some other use cases of __new__
magic method?
For those who want to build a career in DS/ML on core expertise, not fleeting trends:
Every week, I publish no-fluff deep dives on topics that truly matter to your skills for ML/DS roles.
For instance:
Conformal Predictions: Build Confidence in Your ML Model’s Predictions
Quantization: Optimize ML Models to Run Them on Tiny Hardware
5 Must-Know Ways to Test ML Models in Production (Implementation Included)
Implementing Parallelized CUDA Programs From Scratch Using CUDA Programming
And many many more.
Join below to unlock all full articles:
SPONSOR US
Get your product in front of ~90,000 data scientists and other tech professionals.
Our newsletter puts your products and services directly in front of an audience that matters — thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., who have influence over significant tech decisions and big purchases.
To ensure your product reaches this influential audience, reserve your space here or reply to this email to ensure your product reaches this influential audience.
Greetings Dear Avi Chawla,
I am Mirsha, from India, a B.Tech in Artificial Intelligence and Data Science pursuing student, and now I am preparing for my placements, can you please make the OOPS in python In-Depth article free again. Please ... I need that article so bad, and I cant support your works at the moment, please make this OOPS article free or atleast provide me access to it...This is my mail id "a.k.mirsha9@gmail.com".
Thank you.