+ 1
if __add__ is used to add another num to the exsisted num, so is __sub__ , __mul__, etc. then why do i keep seeing this:
def __add__(other, x, y): return x - y (Or sth similar)
3 odpowiedzi
+ 2
When used with the operator '+', Python only sends two parameters:
1. the class instance making the request (here named 'other', normally 'self' by convention)
2. the value on the other side of the '+' sign.
Not having seen the challenge, compare using '+' and __add__() directly:
class NewInt(int):
def __add__(other, x, y):
return x - y
eight=NewInt(8)
print(eight.__add__(3,4))
# -1
print(eight + 3)
# TypeError: missing 1 required positional argument: 'y'
It's hard to say what's right/wrong without the challenge (con)text.
+ 1
Where are you seeing this (context request)?
0
In the challenge. Is there sth wrong with that code?