what is magic method and why is wriiten as method?
Magic methods in iterator
@aamiropti In other languages such as C++ we have a concept of Operator Overloading , similarly in python we have magic methods.
Magic Methods-
Magic methods are always called whenever we use any operator such as +,-,* and so on or even when we use any in buit function such as int(),float() which are used for type conversions.
Whenever you add two numbers using + operator a method called __add__(self)
is called internally , this method then explains the system “What is to be done.” whenever + is encountered in our code.
Similarly when we call any in built function such as abs()
to find the absolute of a number or float()
to convert a variable into float , functions __abs__(self)
and __float__(self)
are called internally to perform their functionality.
Why are they called methods-
Because as we know everything in python is an object. Which means that there exists a class in which they are stored.
These magic methods are also a part of class which is inbuilt in python, and as we know functions in a class are called methods .
In case you still have any doubt feel free to ask it in the same thread.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.