One of the things I (and many Python programmers) love the most about Python OOP is its Magic Methods.
Magic Methods offer immense flexibility to define the behavior of class objects in specific scenarios.
They are prefixed and suffixed with double underscores, such as __len__,
__str__
, and many more.
On a side note, that is why they are also called “Dunder methods” — short for Double UNDERscore.
They make our classes more intuitive and easier to work with.
As a result, awareness about them is extremely crucial for developing elegant and intuitive pipelines.
The visual below summarizes the 20 most used Magic Methods in Python.
In my experience, these are possibly the only 20 magic methods you would ever need in most Python projects utilizing OOP.
__new__
:This method is invoked before
__init__
to allocate memory to an object.In most cases, we wouldn’t need it.
Yet, at times, I have used this to define checks and allocate memory only when certain conditions are met.
Another common usage is to define singleton classes — classes with only one object.
__init__
:Most common in this list.
This is invoked after memory allocation to assign value to an object’s attributes.
__str__
:Executing
print(obj)
outputs the memory address of the object, which is not interpretable.Defining this method lets us print the object in a readable format.
__int__
: Invoked when we execute →int(obj)
.__len__
: Invoked when we execute →len(obj)
.__call__
:Invoked when a class object is called as a function →
obj()
.
__getitem__
: Invoked when an object is indexed →obj[key]
.__setitem__
: Invoked when an object is indexed and a value is set →obj[key]=value
.__delitem__
: Invoked when an object’s index is deleted →del obj[key]
.__contains__
: Invoked when thein
operator is used →item in obj
.__bool__
: Invoked when an object is used in a boolean context →if obj or bool(obj)
.__iter__
: Invoked when we iterate over an object →for x in obj
.__eq__
: Invoked when==
operator is used to compare two objects →obj1 == obj2
.__ne__
: Invoked when!=
operator is used to compare two objects →obj1 != obj2
.__gt__
:Invoked when
>
operator is used to compare two objects →obj1 > obj2
.Other than
__gt__
, we also have:__lt__
: less than.__le__
: less than or equal to.__ge__
: greater than or equal to.
__add__
: Invoked when two objects are added →obj1 + obj2
.__mul__
: Invoked when two objects are multiplied →obj1 * obj2
.__abs__
: Invoked when we compute the absolute value of an object:abs(obj)
.__neg__
: Invoked when the unary operator-
(minus) is used on an object →-obj
.__invert__
: Invoked when~
(tilde) operator is used to invert an object →~obj
.
That’s it.
Now you know the most common Magic methods in Python and how they are used.
👉 Over to you: Did I miss any common magic methods? If yes, which one(s)? Let me know :)
👉 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:
You Cannot Build Large Data Projects Until You Learn Data Version Control!
Why Bagging is So Ridiculously Effective At Variance Reduction?
Sklearn Models are Not Deployment Friendly! Supercharge Them With Tensor Computations.
Deploy, Version Control, and Manage ML Models Right From Your Jupyter Notebook with Modelbit
Gaussian Mixture Models (GMMs): The Flexible Twin of KMeans.
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!
Great posts as always, consistent useful information ! Congratulations and thank you !
Hi Avi/Author,
The syntax column of setitem has __getitem__ method in the image shown. Kindly fix it.