+ 2

I don't understand python's 3 magic methods! Please someone help me!

15th Mar 2019, 6:41 AM
123 456
123 456 - avatar
2 odpowiedzi
+ 7
These so-called "magic methods" have actually nothing to do with wizardry, lol. Okay, they're just special methods with special kind of names. Like, __init__ is a magic method and since it has double underscores on the both side, they're also called dunders. They comes to handy when you need to overload basic functionalities according to your object. You might have seen in Python, using '+' in between two numbers results in returning a third number which is obv. the sum of two but when you use the same '+' between two strings, it concatenates them! ...ever wondered why? It's bcz functionality of '+' can be altered by overloading __add__ magic method and so it's been overloaded so for string objects. There are many "magic methods" available including __init__ __add__ __sub__ __mul__ __floordiv__ __truediv__ __mod__ __and__ __or__ __eq__ __neq__ And many more You can read more at: https://realpython.com/operator-function-overloading/
15th Mar 2019, 8:36 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
Thanks!
15th Mar 2019, 9:11 AM
123 456
123 456 - avatar