operator overloading in python
Python Tutorials

Operator Overloading in Python

Operator Overloading in Python

Operator Overloading in Python is used to achieve the Compile Time Polymorphism.

Operator Overloading is an important feature of Python Programming Language and also of Object Oriented Programming Language like C++.

Questions based on Operator Overloading in Python are generally asked in the  Technical Interview of Software Engineering Jobs .

Today in this tutorial we are going explain Operator Overloading in Python programming with suitable example.

Frequently Asked Questions

After reading this tutorial students can answer the questions related to operator overloading in python.

  • What is meant by Operator Overloading ?
  • Why Operator Overloading is needed ?
  • What are characteristics of operator overloading ?

Let’s Start with introduction of operator overloading.

What is Operator Overloading ?

  • Operator Overloading is the ability of a single operator to perform more than one operation based on the type of operand.
  • We can understand this definition with the help of a simple example. For example + operator can be used to add two numbers , concatenate two string and also used to merge two list.
  • In the same we can define additional methods for these operator to extend their functionality to various new classes. This process is known as Operator Overloading.
  • Generally, Python operators work for built-in classes. But some operators behave differently with different type of data type.
  • For example, the ‘+’ operator will perform addition operation on two numbers (int/float), merge two lists (array) and concatenate two strings.
  • If any operator performs additional actions other than what it is meant for, then it is called operator overloading.
  • If we are using ‘+’ operator for adding two numbers, int.__add__(self, other) method is called when we try to add two numbers using ‘+’ operator. ‘+’ operator makes it easier for us to add two numbers without calling the method again and again.

Operating Overloading in Python Example

An example of Operator Overloading in Python for plus + operator is explain here. Let’s understand this program for operator overloading in python.

Example 1 

x=10+20

y=int.__add__(10, 20)

print(“adding with + operator”, x)

print(“adding by calling __add__ method”,y)

Output

adding with + operator 30

adding by calling __add__ method 30

Now understand use of + operator with string value in the following example

x=”string “+”concatenation”

y=str.__add__(“string “, “concatenation”)

print(“concatenating with + operator”, x)

print(“concatenating by calling __add__ method”,y)

Output

concatenating with + operator string concatenation

concatenating by calling __add__ method string concatenation

Operator Overloading in Python Example 3

But if we try to concatenate an integer with a string, python will throw an error, because there is no method that can concatenate an integer and a string together.

x=10+”concatenation”

print(“concatenating with + operator”, x)

Output

x=10+”concatenation”

Output

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Operator Overloading in Python Example 4

In this example + operator is overloaded for Adding two objects

class A:

def __init__(self, other):

self.x= other

class B:

def __init__(self, other):

self.x=other

a=A(500)   #defining the onjects

b=B(700)

print(a+b)

Output

TypeError: unsupported operand type(s) for +: ‘A’ and ‘B’

In the above code, python throws an error as “unsupported operand type(s) for +: ‘A’ and ‘B’”. That is because, there is no __add__ method defined for class A and class B.

Hence the objects ‘a’ and ‘b’ doesn’t recognize the ‘+’ operator and throws a

TypeError.

class A:

def __init__(self, other):

self.x= other

def __add__(self, other): #defining the __add__ method here

return self.x+other.x

class B:

def __init__(self, other):

self.x=other

a=A(500)   #defining the onjects

b=B(700)

print(“adding two objects gives “,a+b)

class Operatoroverload:

def __init__(self, a, b):

self.a = a

self.b = b

# adding two objects

def __add__(self, other):

return self.a + other.a, self.b + other.b

Ob1 = Operatoroverload(1, 2)

Ob2 = Operatoroverload(2, 3)

Ob3 = Ob1 + Ob2

print(Ob3)

Output

(3, 5)

Example 4 

class Compare:

def __init__(self, a):

self.a = a

def __gt__(self, other):

if(self.a>other.a):

return True

else:

return False

ob1 = Compare(62)

ob2 = Compare(27)

if(ob1>ob2):

print(“ob1 is greater than ob2”)

else:

print(“ob2 is greater than ob1”)

Output

ob1 is greater than ob2

Conclusion and Summary 

We have discussed the Operator Overloading in Python with example. I hope this tutorial will be helpful for you in understanding the operator overloading in python concept.

Also Read – Python Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *