In my last post, I talked about Object-Oriented Programming(OOP). And I specifically talked about a single magic method __init__
which is also called as a constructor method in the OOP terminology.
The magic part of __init__
is that it gets called whenever an object is created automatically. But it is not the only one in any sense. Python provides us with many other magic methods that we end up using without even knowing about them. Ever used len()
, print()
or the []
operator on a list? You have been using dunder methods.
In this post, I would talk about five of the most used magic functions or “dunder” methods.
__add__ , __sub__ , __mul__ , __truediv__ , __lt__ , __gt__
Everything in Python is an object, ranging from the data types int
,str
,float
to the models, we use in data science. We can call methods on an object, like this str object
Fname = "Rahul"
## Now, we can use various methods defined in the string class using the below syntax
Fname.lower()
#data-science #python #artificial-intelligence #machine-learning #programming