What is the use of magic method __add__?? In below code why result not returning addition of first and second??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the use of magic method __add__?? In below code why result not returning addition of first and second???

class test: def __init__(self,x,y): self.x=x self.y=y def __add__(self,other): return test(self.x*other.x, self.y*other.y) first=test(4,7) second=test(5,6) result=first+second print(result.y)

21st Feb 2018, 5:48 PM
Pankaj Kumar
Pankaj Kumar - avatar
1 Answer
+ 7
if you use __add__ the method must return sum like def __add__(self,other): return self.x+other.x and not coma sepereted like you did
25th Feb 2018, 9:26 AM
__Nick
__Nick - avatar