Discover more from Daily Dose of Data Science
A Consolidated List of 20 Most Common Magic Methods
Magic methods: The gem of Python OOP.
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
__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.
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.
Most common in this list.
This is invoked after memory allocation to assign value to an object’s attributes.
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 →
__len__: Invoked when we execute →
Invoked when a class object is called as a function →
__getitem__: Invoked when an object is indexed →
__setitem__: Invoked when an object is indexed and a value is set →
__delitem__: Invoked when an object’s index is deleted →
__contains__: Invoked when the
inoperator 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.
>operator is used to compare two objects →
obj1 > obj2.
__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:
__neg__: Invoked when the unary operator
-(minus) is used on an object →
__invert__: Invoked when
~(tilde) operator is used to invert an object →
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 :)
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (550+ pages) with 320+ tips.
👉 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!