+ 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)

4th Nov 2017, 2:48 AM
monotom
monotom - avatar
3 Answers
+ 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.
4th Nov 2017, 4:09 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Where are you seeing this (context request)?
4th Nov 2017, 3:17 AM
Kirk Schafer
Kirk Schafer - avatar
0
In the challenge. Is there sth wrong with that code?
4th Nov 2017, 3:27 AM
monotom
monotom - avatar