+ 1

Oop Doubt Python

Hey everybody, I recently started learning the OOP concepts but had a doubt in magic methods. I understood the methods such as __init__ , __add__, __ge__ etc which get triggered when we use their symbol(=,+,<= etc). But the methods such as __len__, __int__, __iter__ confuse me. Firstly how to trigger these functions?, secondly,when to use these instead of a normal method(one we created)? I hope you all understood my question.

10th Sep 2020, 4:24 AM
Kunsh-Tyagi
Kunsh-Tyagi - avatar
4 odpowiedzi
+ 4
Magic methods are just simple methods which make an object capable of reacting to an operator or a function in a different way. Hope this works. https://www.tutorialsteacher.com/JUMP_LINK__&&__python__&&__JUMP_LINK/magic-methods-in-python
10th Sep 2020, 4:28 AM
Charitra
Charitra - avatar
+ 1
These methods will be triggered when you use built-in functions. For example if you use len() function the __len__ method will trigger on the object you passed as a parameter to len function.
10th Sep 2020, 7:57 AM
gambler
+ 1
And __str__ is triggered when you use str function. This happens implicitly, for example, when you print something.
10th Sep 2020, 12:04 PM
HonFu
HonFu - avatar
0
Charitra Aggarwal i actually wanted to know that how do the methods like __len__, __str__ etc get triggered
10th Sep 2020, 7:41 AM
Kunsh-Tyagi
Kunsh-Tyagi - avatar