Reputation: 51
I am learning python currently and recently have come across the term magic methods. I have come to know that they are the dunder methods that we use inside the class i.e. __init__ or __add__ or __repr__.
Also they can used for operator overloading. Like for addition with the help of
__add__.
However i am still confused as what they really are? Are all methods (double underscore) magic methods?
Or is there anything i am missing out. If so why are they called MAGIC METHODS, there must be something that i am not quite getting.
I do know now that magic methods are used to add more functionalities to the class instead of the instance of the class. i.e object. Is that what they are actually created for along with operator overloading?
Upvotes: 1
Views: 318
Reputation: 5140
It is not stated / referred as dun dun method. But is dunder which means double under (underscores).
Consider dunder
as a python idiom for double underscore.
E.g: __init__
is spoken / read as dunder init. You can follow same phrasing pattern for other magic methods such as __add__
as dunder add.
There is good reference on magic method here which is a good point to start.
Upvotes: 1