+ 4

Hi everybody! Why the output of this code is 9?

https://code.sololearn.com/cDMbhQjzDe9q/?ref=app

18th May 2019, 8:59 PM
Harimamy Ravalohery
Harimamy Ravalohery - avatar
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)
18th May 2019, 9:09 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 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.
18th May 2019, 9:48 PM
Qasem
+ 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
18th May 2019, 9:08 PM
KrOW
KrOW - avatar