Tutorial 9 : OOPs in Python — Magic methods or Dunder methods

Gaurav Patil
3 min readFeb 8, 2022
Photo by Hitesh Choudhary on Unsplash

Missed previous lecture ?? — Click here

Feel free to navigate directly to any topic given below :

Using built-in functions for user-defined class objects

magic method __str__() for user-defined class objects

magic methods __int__() and __float__() for user-defined class objects

magic methods __add__() and __mul__() for user-defined class objects

magic methods __lt__() , __ge__() , __ne__() for user-defined class objects

Using built-in functions for user-defined class objects

We cannot use built-in functions for user-defined objects. So we use magic method solve this issue.

Magic methods are preceded and followed by two underscores.

magic method __str__() for user-defined class objects

magic methods __int__() and __float__() for user-defined class objects

magic methods __add__() and __mul__() for user-defined class objects

magic methods __lt__() , __ge__() , __ne__() for user-defined class objects

Next tutorial is about more of such Magic methods with many examples.

So we come to the conclusion that, in order to make Python’s built-in functions to work for user-defined class objects we need to make use of magic methods.

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.