0

Magic Methods and Operator Overloading

For the example problem in Magic Methods and Operator Overloading, Slide 1, I do not understand how we got 8 and 16. I tried debugging it, and all I got were references like <__main__.Vector2D object at 0x039D6FB0>, which I do not understand. Can someone please explain to me what Magic Methods and Operator Overloading is?

24th Dec 2018, 9:40 PM
BitsAndBytes
BitsAndBytes - avatar
1 Resposta
+ 5
You mean this, right? https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2470/ In every language there are operators that are used to work with data, but what they actually do, depends on the data type. For example if you write 5+5, the result is 10, but if you write '5' + '5', the result is '55'. Operator overloading means to define what an operator does in your self-designed data type. In Python you have reserved names for the methods made to overload operators, and the name of the overload of '+' is __add__. You could define anything, even nonsense, like that if you use + on your type, a subtraction is executed. Anyway, these methods (with their pre-defined names) to define what built-in functionality of Python does in case of your type, are called Magic Methods. Please take a look at the example and see if you understand it better now!
24th Dec 2018, 9:57 PM
HonFu
HonFu - avatar