What is the uses of magical methods in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the uses of magical methods in python?

Real time use case of magical method in python where magical method is really helpful or a use case which can be achieved by magical method only.

24th Apr 2017, 11:04 AM
Prakhar Jain
Prakhar Jain - avatar
3 Answers
+ 11
you have to draw a pentagram with beetle juice and chant import this 999 times and then summon the python spirit who will tell you to meet monty in britian. monty will bring you to Hogwarts and there you can learn the magical method
24th Apr 2017, 11:18 AM
Ahri Fox
Ahri Fox - avatar
+ 6
On top of what Ahri suggested, they *also* might be used in class definition. You for example always define the __init__ magic method. Apart from defining the class's features, you may for example tell it to increment a counter each time an object is instantiated. So you can keep track of how many of such objects were created already. But really vast applications of this - you usually define __str__ or __repr__ which define how the print(object_name) should behave. You can overload standard operators, customize attribute access and many more.
24th Apr 2017, 1:52 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
If you make a class for vectors, you likely want to add them. So use magic method to get + on vectors.
24th Apr 2017, 10:00 PM
1of3
1of3 - avatar