Why is a() + b() translated to b.__radd__(a()) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is a() + b() translated to b.__radd__(a())

This is from the magic methods lesson question 2. I keep thinking the add function/method is a part of a when you call it, so it should be a.__radd__(b()) right?

16th Apr 2019, 7:32 PM
Avery Finchum
Avery Finchum - avatar
1 Answer
+ 8
a() + b() is translated to a.__add__(b()). __radd__ is "reverse add". The lesson says that if __add__ hasn't been defined, it will look for __radd__ and translate a() + b() to b.__radd__(a()).
16th Apr 2019, 7:45 PM
Russ
Russ - avatar