__add__ and __radd__ | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

__add__ and __radd__

can someone explain me the differences between __add__ and __radd__ with the help of a simple program.

9th Jun 2021, 12:54 PM
kushal
6 ответов
9th Jun 2021, 5:12 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Check the following code example, https://code.sololearn.com/cK03N0e1YE9U/?ref=app
9th Jun 2021, 1:48 PM
Abhay
Abhay - avatar
+ 1
__add__ stand for "addition" __radd__ stand for "right addition" default addition of a+b is done by calling a.__add__(b) so technically __add__(a,b)... if class of a doesn't implement the __add__ method, then if class of b has __radd__ method, a+b us rather done by calling b.__radd__(a) so technically __radd__(b,a)...
9th Jun 2021, 4:38 PM
visph
visph - avatar
0
Runtime Terror can you explain using a program .?plz. I did not get what you said😅
9th Jun 2021, 4:09 PM
kushal
0
visph can we use __radd__ method for the objects of same classes ? If yes then how ?
10th Jun 2021, 4:28 AM
kushal
0
kushal no, for same class addition, the __add__ method only is called... even if you implement only __radd__ (and not __add__), you'll get an argument error ^^
10th Jun 2021, 4:32 AM
visph
visph - avatar