Lumos!!

We will shine light on one very fascinating aspect of Python today – magic methods. These methods are the methods which make magic happen in Python Classes. For example, even if you do a simple task in Python like,


>>> 6 + 7
11


magic methods come into play to make the actual addition. Let’s get into details.

First things first, we need to understand that everything in Python is object defined by a class. An integer number is defined by class int. And the numbers we used before (6 and 7) are the instances of this class. That’s it, now we can get into magic methods.

Magic methods are methods of classes that start and end with double underscores (__). If you are familiar with OOP concepts with Python, one of the most common example of the magic method is the constructor of any class (__init__). If we do dir() on any number, we can see all the methods available including magic methods.

 

Python already supplies a lot of magic methods that are inherited from base object class.

When we did 6+7=11 in the example above, behind the scenes, another magic method (__add__) gets called which returns the sum of the two numbers. The + operator calls the __add__ function as 6.__add__(7) which returns us the sum of 6 and 7. The magic methods are not called directly, and should not be called directly either. They are invoked by some other actions. Like: __init__ is called when a class is instantiated, or __add__ is called by the operator +.

Lets see the example below.

But if we perform the addition of these two distances?

 

That’s because, the Measurement class we created does not know what to do when it is operated by +. We can define __add__ magic method to perform this task.

Now, if we do the following:

Notice that the result of addition depends on the operand on the left hand side. If you want to get result based on operand on right hand side, use __radd__ method instead of __add__.

Lets do one more example. This time lets overload comparison operators.

 

Some other magic methods for operator overloading and build-in function overriding are listed as below.

__abs__ – To be called on abs() function
__round__ – To be called on round() function
__floor__ – To be called on math.floor() function
__ceil__ – To be called on math.ceil() function
__le__ – To be called by <= operator
__ne__ – To be called by != operator
__eq__ – To be called by == operator

Now, another greatest use of magic methods are in creating a context manager. We will get into context managers in next article, for now let’s just look into the basic stuff. To put it in simple terms, context manager are used with with statement.

Lets create a not so useless context manager just to demonstrate how its done. We need to define two magic functions __enter__ and __exit__. The __enter__ will describe what is done when the context manager is instantiated and __exit__ will describe when the it needs to exit.

That’s it for today!! Hope you learned something. If you have any doubt or confusion, don’t hesitate to get in touch with us.