In Python, we sometimes see method names with __
around, such as the __init__
method that every Class has. These methods are “dunder” methods (“dunder” stands for “double under” or “double underscore”). Dunder methods in Python are used for operator overloading and customizing behavior of other functions.
Sometimes dunder methods are also called “magic methods” because they are usually called by Python under the hood. But they are not really magical, you can define dunder methods to customize the behavior of your own classes.
Examples of Dunder Methods
In the following example, we can see three dunder methods:
__init__
method: is called to initialize the class__str__
method: is called when converting the object to a human-readable string__repr__
method: is called when converting the object to a developer-readable string
1 2 3 4 5 6 7 8 9 |
|
In Python, many dunder methods are implemented and used for operations such as arithmetic operators, comparison operators, truthiness, etc. The following are a few examples:
Arithmetic Operators
+
:__add__
-
:__sub__
*
:__mul__
/
:__div__
+=
:__iadd__
-=
:__isub__
Comparison Operators
x < y
:x.__lt__(y)
x <= y
:x.__le__(y)
x > y
:x.__gt__(y)
x >= y
:x.__ge__(y)
x == y
:x.__eq__(y)
x != y
:x.__ge__(y)
If you know about Bash shell, you may notice the name of these dunder methods are very similar to the operators in Bash.
Truthiness
bool(x)
:x.__bool__()
Use Dunder Methods to Customize Class Behaviors
Dunder methods provide a way for our class to customize operators and other built-in Python behavior for our objects. In the following two examples, I’ll use dunder methods to overload arithmetic operators, and implement a dictionary that can be used with both attribute and item syntax.
Example 1: Overload Arithmetic Operators
Make an is_callable
function to check if an object type is callable.
This can be done by calling object’s __call__
method.
Example:
1 2 3 4 5 6 |
|
Source Code:
1 2 3 4 5 6 7 8 9 10 |
|
Example 2: Class EasyDict
Make an EasyDict
class that can be used with both attribute and item syntax.
This can be done by implementing __getitem__
and __setitem__
methods.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
|
Source Code:
1 2 3 4 5 6 7 8 9 |
|
More dunder methods can be found in Python documents: operator – Standard operators as functions (Python 3.6.3)