Python magic methods or Dunder methods
Python magic methods are the functions which are predefined in python and performs some special task. These are surrounded by the double underscores that is why these are also called “Dunder methods” . These methods let us simulate the python’s built in behaviour. Some common dunder functions are __init__, __str__, __repr__ , __del__ .
In this article we will learn:
- What are magic functions?
- What is operator overloading?
What is operator overloading ?
Operator overloading is a specific case of OOP polymorphism in which some or all operators like +, = or == are treated as polymorphic functions and as such have different behaviors depending on the different situations.
In above example, there are four variables, two of them are type integers and other type string. So the operator “+” performs different operations (Addition in case of integer values and concatenation in case of string values), so this concept is known as Operator overloading. This concept becomes more familiar to us with magic functions.
It is one of the most widely used python magic function , used to initialize values of a class object. The __init__ method calls automatically, when object of class is created. It returns none (nothing). It is used as constructor in python. If you want to learn more about constructor and destructor (__del__) in python then you can visit:
We use this __del__ function to deallocate resources from objects . So it is used as destructor in python. Like constructor, it will call immediately when object is created, so there is no need to call destructor function.
As you can see __init__ and __del__ calls automatically when we create object of class instead of:
__str__ , __repr__
__str__ and __repr__ are python magic methods which are we use to create a string representation of an object. Both of these methods returns a string value.
if we define __repr__ and not __str__ , the object will behave as though __str__ == __repr__
This means, in simple terms: almost every object you implement should have a functional __repr__ that’s usable for understanding the object. Implementing __str__ is optional: do that if you need a “pretty print” functionality (for example, used by a report generator).
In normal words we can say:
- __str__ returns a string which is convenient to end-users
- __repr__ returns a string which is more convenient to debuggers or developers
- If we use __str__ or __repr__ actually what is going on background:
objname.__str__() or objname.__repr__()
It is also an special method, which automatically calls when we create instance of class. __add__ allows us to add some string or integer data to object previous attributes. It allows operator overloading which we discussed earlier. When we call add(), every time there happens following at background:
int.__add__(operand1, operand2,...,operandN) #for integers str.__add__(operand1, operand2,...,operandN) #for strings
To understand how __add__ works, you can directly add variables using above syntax.
int.__add__(10,20) str.__add__('a','b') Output 30 ab
In above example, we have used __add__ to add to class data members. You can also see here __add__ behaves differently in different situation (string and integers) hence operator overloading. If you not use __add__ ( check by remove __add__ function) , then you will see error like this:
Traceback (most recent call last): File "methods.py", line 8, in <module> print(myobj+10) TypeError: unsupported operand type(s) for +: 'Student' and 'int'
It is an magic function, used to print the documentation string of the class or member function. The main purpose to add documentation string in code is to just make code easy to understand by programmers.
We use this function to return the length of string objects or data-members. It woks similar to python’s built in len() method.
"String".__len__() or stringName.__len__() #similar to built in function len('string')
So these are some common python magic methods (or dunder / special methods).