Operator Overloading in Python

Operator Overloading lets you redefine the meaning of operator respective to your class. It is the magic of operator overloading that we were able to use the + operator to add two numbers objects, as well as concatenate two string objects.

Here + operator has two interpretations. When used with numbers it is interpreted as an addition operator whereas with strings it is interpreted as the concatenation operator. In other words, we can say that the + operator is overloaded for int class and str class.

So how do we redefine or overload an operator for a particular class?

Operator Overloading is achieved by defining a special method in the class definition. The names of these methods start and end with double underscores (__). The special method used to overload + operator is called __add__(). Both int class and str class implements __add__() method. The int class version of the __add__() method simply adds two numbers whereas the str class version concatenates the string.

If the expression is for the form x + y, Python interprets it as x.__add__(y). The version of the __add__() method called depends upon the type of x and y. If x and y are int objects then int class version of the __add__() is called. On the other hand, if x and y are list objects then list class version of the __add__() method is called.

The following table lists operator and it’s corresponding special method. Recall from lesson Objects and Classes in Python that the variables names which have two leading underscores are private variables, special methods listed in table are not private because they in addition to leading underscores they also have trailing underscores.

Operator Special Method Description
+ __add__(self, object) Addition
- __sub__(self, object) Subtraction
* __mul__(self, object) Multiplication
** __pow__(self, object) Exponentiation
/ __truediv__(self, object) Division
// __floordiv__(self, object) Integer Division
% __mod__(self, object) Modulus
== __eq__(self, object) Equal to
!= __ne__(self, object) Not equal to
> __gt__(self, object) Greater than
>= __ge__(self, object) Greater than or equal to
< __lt__(self, object) Less than
<= __le__(self, object) Less than or equal to
in __contains__(self, value) Membership operator
[index] __getitem__(self, index) Item at index
len() __len__(self) Calculate number of items
str() __str__(self) Convert object to a string

Notice that the last two items in the table are not operators instead they are built-in functions. But if you want to use them with your class you should define their respective special methods.

The following program first performs an operation using an operator and then it performs the same operation using the corresponding special method.



The following program demonstrates how we can override operators in a class.



The Point class defines two private attributes __x and __y which represent x and y coordinates in a plane. Then it defines getter and setter methods for those attributes. It also defines, get_position() and move() method to get the current position and change coordinates respectively.

In line 35, we have overloaded the + operator for the Point class. The __add__() method creates a new Point object by adding individual coordinates of one Point object to another Point object. Finally, it returns the newly created object to its caller. This allows us to write expressions like this:

where p1, p2 and p3 are three Point objects.

Python interprets the above expression as p3 = p1.__add__(p2), and calls the __add__() method to add two Point objects. The return value from the __add__() method is then assigned to p3. It is important to note that when the __add__() method is called, the value of p1 is assigned to the self parameter and the value of p2 is assigned to the point_obj parameter. The rest of the special methods works in a similar fashion.

2 thoughts on “Operator Overloading in Python

  1. Every Chapter before this i understood. But i did get the code from line 35 till end. This code more explanation. BTW I love this course you taught very well.

Leave a Comment

%d bloggers like this: