An operator is a special character that is used to perform a specific operation on operands. Every operand has provided different functionality like + operator performs addition of two numbers and joining two strings (concatenation), – operator performs subtraction of two numbers, etc. There are many operators predefined in Python. But what if we need to use an operator for a different task. It is possible to give a different meaning to the operator. This is known as operator overloading in Python.
What is operator overloading in Python?
The Python operator overloading gives an extended meaning to the specific operator apart from the predefined meaning of it. The feature of operator overloading allows you to customize the meaning of the operator.
What happens without operator overloading in Python? Let’s consider an example if we want to add two objects of a class with an addition operator.
Example
class add:
x, y = 0
def __init__(self,a,b):
self.x = a
self.y = b
obj1 = add(4,5)
obj2 = add(8,9)
print(obj1+obj2)
Output
Traceback (most recent call last):
File "operator_overloading.py", line 1, in <module>
class add:
File "operator_overloading.py", line 2, in add
x, y = 0
TypeError: cannot unpack non-iterable int object
Since the interpreter shows the traceback error because Python doesn’t know how to add class objects.
How to overload operators in Python?
There are many types of operators that are predefined in Python. Python provides some special functions to overload operators or, in simple words, to give operators different meanings. The function will automatically invoke when associated with the operator. The list of operators and their functions is given below.
Sr no | Operator | Special function |
1 | + | __add__(self,other) |
2 | – | __sub__(self,other) |
3 | * | __mul__(self,other) |
4 | ** | __pow__(self,other) |
5 | / | __truediv__(self,other) |
6 | // | __floordiv__(self,other) |
7 | % | __mod__(self,other) |
8 | += | __iadd__(self,other) |
9 | -= | __isub__(self,other) |
10 | *= | __imul__(self,other) |
11 | **= | __ipow__(self,other) |
12 | /= | __idiv__(self,other) |
13 | //= | __ifloordiv__(self,other) |
14 | >>= | __IRSHIFT__(SELF, OTHER) |
15 | <<= | __ILSHIFT__(SELF, OTHER) |
16 | &= | __IAND__(SELF, OTHER) |
17 | |= | __IOR__(SELF, OTHER) |
18 | ^= | __IXOR__(SELF, OTHER) |
19 | – | __NEG__(SELF, OTHER) |
20 | + | __POS__(SELF, OTHER) |
21 | ~ | __INVERT__(SELF, OTHER) |
Operator overloading in Python example
- Overloading the addition operator
Example
class add:
def __init__(self,a,b):
self.x = a
self.y = b
def __add__(self, second):
x = self.x + second.x
y = self.y + second.y
return x,y
obj1 = add(4,5)
obj2 = add(8,9)
print(obj1+obj2)
Output
(12, 14)
Overload all the arithmetic operators ( +, -, *, /) and perform operations on complex numbers.
Example
class complex:
def __init__(self,r,i):
self.real = r
self.imaginary = i
def __add__(self, other): #overloading addition operator
real = self.real + other.real
img = self.imaginary + other.imaginary
return (real, img)
def __sub__(self, other): #overloading subtraction operator
real = self.real - other.real
img = self.imaginary - other.imaginary
return (real, img)
def __mul__(self, other): #overloading multiplication operator
real = self.real * other.real
img = self.imaginary * other.imaginary
return (real, img)
def __truediv__(self, other): #overloading division operator
real = self.real / other.real
img = self.imaginary / other.imaginary
return (real, img)
print("Enter two complex numbers") #accepting input from user
r1 = int(input("Enter real part of 1st complex number: "))
c1 = int(input("Enter imaginary part of 1st complex number: "))
r2 = int(input("Enter real part of 2nd complex number: "))
c2 = int(input("Enter imaginary part of 2nd complex number: "))
print("First complex number is {} + {}i".format(r1,c1))
print("Second complex number is {} + {}i".format(r2,c2))
obj1 = complex(r1,c1) #creating object of class
obj2 = complex(r2,c2) #creating object of class
a,b = obj1+obj2 #calling the overloaded function
print("Addition of two complex number is: {} + {}i".format(a,b))
a,b = obj1-obj2 #calling the overloaded function
print("Subtraction of two complex number is: {} + {}i".format(a,b))
a,b = obj1*obj2 #calling the overloaded function
print("Multiplication of two complex number is: {} + {}i".format(a,b))
a,b = obj1/obj2 #calling the overloaded function
print("The division of two complex number is: {} + {}i".format(int(a),int(b)))
Output
Enter two complex numbers
Enter real part of 1st complex number: 10
Enter imaginary part of 1st complex number: 20
Enter real part of 2nd complex number: 2
Enter imaginary part of 2nd complex number: 5
First complex number is 10 + 20i
Second complex number is 2 + 5i
Addition of two complex number is: 12 + 25i
Subtraction of two complex number is: 8 + 15i
Multiplication of two complex number is: 20 + 100i
The division of two complex number is: 5 + 4i