0
Dunders and Magic Methods - "other"?
In the coded example I am given in, it reads: def __add__ (self, other): Does the other "self" NEED to be called "other" or not? Thanks 🙂
3 odpowiedzi
+ 2
Neither of them NEED to be called that, it just really helps when writing code and its standard.
For example think of a point. A tuple with an x and y coordinate. Theres not a good way to add them in python, but what if we made a "Point" class and overloaded the + opoerator?
...
...
def __add__(self, other):
p3 = Point(self.x + other.x, self.y + other.y)
+ 1
I really didn't get what you mean so here is a simple code of adding two numbers
class adding():
def __init__(self,x, y):
self.x=x
self.y=y
def __add__(self,second):
return self.x+second.x
a=adding(5,6)
b=adding(6,7)
print(a+b)
+ 1
Thanks for the answers!