Tutorial 9 : OOPs in Python — Magic methods or Dunder methods
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 objectsmagic methods
__int__()
and__float__()
for user-defined class objectsmagic methods
__add__()
and__mul__()
for user-defined class objectsmagic 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.