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)
2 ответов
+ 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.
0
Thank you!