How the __add__ dunder method is connected to the "+" operator in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How the __add__ dunder method is connected to the "+" operator in python?

Hey everyone, I have been learning about the operator overloading in python. I find it useful and interesting but there is still one thing I wanna know So behind the scenes when there is any operator the python will call corresponding dunder method for that operator for ex:- whenever python sees the + sign it calls the __add__ method, __mul__ for * sign etc. But how python knows to call __add__ whenever there is + operator? (Ex:- a + b, whenever python encounter this expression, it will call __add__ method on a, it becomes a.__add__(b) behind the scenes) (Is there some class that sees for operators and then calls correct Dunder method?)

10th Feb 2022, 5:29 AM
Sandeep
Sandeep - avatar
0 Answers