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 🙂

13th May 2020, 10:22 AM
Teeny Sanguini
Teeny Sanguini - avatar
3 Answers
+ 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)
13th May 2020, 10:27 AM
Slick
Slick - avatar
+ 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)
13th May 2020, 10:28 AM
Abhay
Abhay - avatar
+ 1
Thanks for the answers!
13th May 2020, 3:43 PM
Teeny Sanguini
Teeny Sanguini - avatar