Operator Overloading in Python

How to make your object play well with built-in operators

Aditya Dhanraj Tiwari
Python in Plain English

--

pic credits LocalNav

Each operator can be used in different way for different types of operands. for example when + is used with integers, it add integers to give result and when + is used with string, it concatenates the provided strings.

x, y = 10, 20
print(x + y) # 30
a,b = 'John', 'Wick'
print(a + b) # John Wick

This change in behaviour of + operator according to type of operands provided, is called Operator overloading

Operator overloading refers to the ability of operator to behave differently depending on operands it acts on.

Can + operator add any two object? Let’s see.

Running the above will give you error. why? because + does not know how to add two objects of Vector class.

In this article we will explore how to make built-in operator play well with user defined objects, keeping limitations Python imposes on operator overloading in mind.

These limitations are →

  1. You can not create new operators, only overloads existing ones
  2. You can not overload operators for built-in types like tuple, string, list.
  3. Few operators can not be overloaded like is, or, and, not

To start with Operator overloading, we need to know what is Python Data model and Special methods.

Think Python data model as an ‘Python Design’ or ‘Python Framework’. It tells you how python manages objects and perform operation on them. it describes an Api which you can use to make your objects work like built-in type and use most of the idiomatic features of python language without implementing them.

Special methods → When you use len(collection) , python interpreter invoke collection.__len__() method. Here __len__() is a special method.

Special method starts with __ and end with __ and are meant to be invoked by python interpreter only, not by you unless you are doing metaprogramming.

Using special methods we can achieve operator loading in Python.

Now make our Vector class support + operator

When we call + operator, Python interpreter invoke __add__(self, other) special method. In that case, to support + operator we need to implement __add__() special method in our class.

Now you can see our class support + operator , we have also implemented __repr__() method, when we use print(), interpreter invoke __str__() method, if it is not implemented it will call __repr__() method.

Note: Always return new object when overloading operator unless you are overloading augmented assignment like += operator.

Take one more example of overloading operator

When we use == operator, Python interpreter invoke __eq__(self, other) special method, in that case by implementing __eq__() method we can support == operator.

Operator overloading in python is easy to implement using special methods. The key is to understand the Python data model and Special methods.

These concepts allow you to get benefit of python idiomatic features and leverage powerful python standard library.

To understand python data model in more detail please refer Fluent python.

Please find list of special methods in Python here.

Summary

We started by understanding what Operator Overloading is. Then reviewed restrictions imposed on operator overloading in Python, data model and Special methods, after that we implemented operator overloading for our Vector class.

I hope you have enjoyed this article. Good Luck, Happy Coding 😃

--

--