0

Wat Use?

Actually the __add__ method... we can simply calculate without using that method... I thought... but Wat is the actual use by using tat method?

2nd Dec 2018, 8:30 AM
Sharuk khan
Sharuk khan - avatar
1 Antwort
+ 2
__add__ is basically special method for special works.mostly people also called these functions magic methods. Why we use this.when you want to add two objects then you use __add__ method and for subtract you use __sub__ and so on. Like this. class j(): def __init__(self,x): self.x=x def __add__(self,other): print(self.x+other.x) x=j(3) #this is self.x y=j(4) #this is other.x print(x+y) #output=7 #here you add two objects of j() class with the help of __add__ method you can't do this without add.if you don't define __add__ method then you will got error.
2nd Dec 2018, 9:16 AM
Maninder $ingh
Maninder $ingh - avatar