- Overload Operators in Python
+Operator in Python
>Operator in Python
==Operator in Python
Operator overloading means changing the way operators behave in different situations. It is a type of polymorphism.
With operator overloading, we can add extra meaning or functionality to an operator to perform more than one operation.
For example, the
+ operator performs addition with integer operands. But when used with string operands, it performs concatenation because the
+ operator is overloaded.
Overload Operators in Python
Python provides a way to overload operators with user-defined data types through the Magic methods. These are special methods automatically invoked when we use the operator associated with them.
They are also known as Dunder methods because they start and end with a double underscore.
For example, when we write the operator
+, the magic method
__add__() is automatically called on the backend. Therefore, if we want to perform operator overloading with certain operators, we only have to change the code of their magic methods.
+ Operator in Python
In the following code, we have a user-defined class in which we have implemented the magic method
__add__() to overload the
When we write
int1 + int2, actually on the back-end the magic method
__add()__ is called as
int1.__add__(int2). The same working goes for the strings as well.
When we write
str1 + str2, internally the magic method is called as
# Python 3.x class MyAddion: def __init__(self, a): self.a = a def __add__(self, b): return self.a + b.a int1 = MyAddion(2) int2 = MyAddion(2) str1 = MyAddion("Delft") str2 = MyAddion("Stack") print(int1 + int2) print(str1 + str2)
#Python 3.x 4 DelftStack
> Operator in Python
Here, we have overloaded the
> operator in the
MyComp class. When we write the operator
>, the magic method
__gt__() is invoked internally.
So when we write
int1 > int2, the magic method
__gt__() is called as
int1.__gt__(int2). This method returns
true if the condition is true; else, it returns
# Python 3.x class MyComp: def __init__(self, a): self.a = a def __gt__(self, b): if self.a > b.a: return True else: return False int1 = MyComp(5) int2 = MyComp(3) if int1 > int2: print(int1.a, "is greater than", int2.a) else: print(int2.a, "is greater than", int1.a)
#Python 3.x 5 is greater than 3
== Operator in Python
Here, we have overloaded the equality operator
== in our class
MyEquality. When we write
int1 == int2, internally the magic method
__eq__() will be invoked as
If both operands are equal, it will return
Equal; else, it will return
# Python 3.x class MyEquality: def __init__(self, a): self.a = a def __eq__(self, b): if self.a == b.a: return "Equal" else: return "Not equal" int1 = MyEquality(3) int2 = MyEquality(3) print(int1 == int2)
#Python 3.x Equal
It’s worth noting that we cannot change the number of operands for an operator in case of operator overloading. We cannot overload a unary operator as a binary operator and vice versa.
For example, we cannot overload the
~ invert operator (a unary operator) as a binary operator.