0

why 9 output of this code

class wow: def __init__(s, x): s.x = x def __add__(s, y): return wow(s.x - y.x) def __sub__(s, y): return wow(s.x + y.x) def __repr__(s): return str(s.x) a = wow(5) b = wow(7) c = wow(3) print(a - b + c)

20th Jul 2018, 3:31 AM
khjoony
khjoony - avatar
2 Answers
+ 8
Because __sub__ is defined as addition (+) and __add__ as subtraction (-). As a result, 5-7+3 is evaluated as 5+7-3 which results in 9.
20th Jul 2018, 3:38 AM
Hatsy Rei
Hatsy Rei - avatar
0
Thank you!
23rd Jul 2018, 3:30 AM
khjoony
khjoony - avatar