Oop Doubt Python | Sololearn: Learn to code for FREE!

+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.

9/10/2020 4:24:00 AM

Kunsh-Tyagi

4 Answers

New Answer

+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/python/magic-methods-in-python

+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.

+1

And __str__ is triggered when you use str function. This happens implicitly, for example, when you print something.

0

Charitra Aggarwal i actually wanted to know that how do the methods like __len__, __str__ etc get triggered