+ 4
Hi everybody! Why the output of this code is 9?
3 RĂ©ponses
+ 10
obj1 + obj2 calls obj1.__add__(obj2) and obj1 - obj2 calls obj1.__sub__(obj2)
so wow(5) - wow(7) returns wow(12) and wow(12) + wow(3) returns wow(9)
+ 7
It's operator overloading concept. With magic methods (those start and end with double underlines) you can change behaviour of math operators such as addition and subtraction and ... on your class onjects. In this case the addition overloaded to subtraction and vice versa.
+ 5
Because your code do the opposed operation on addition and subtraction (eg when you add you subtract the x field, when you subtract you add the x field). Then, a - b + c is same like 5 + 7 - 3 => 9