Operator Overloading is changing the purpose of usage of the operators as the type of operands is changed.

Let’s take example of ‘+’ operator, then it will add two numbers and it will also concatenate the strings. So, as the type of operands gets changed the operators changes its behaviour during evaluation.

Similarly goes for ‘‘ if we are multiplying numbers using ‘‘ then it will perform mathematics multiplication and the same operator(‘*’) when apply with string and number then output will change.

Checking these concepts with code snippets -:

Operator Overloading in inbuilt operators

How ‘+’ operator is overloading.

In [1]:

## For numbers, + operator  is performing addition
print(2+3)

5

In [2]:

## For strings, + operator is concatenating two strings
'Welcome '+'to course!'

Out[2]:

'Welcome to course!'

How ‘*’ operator is overloading

In [3]:

## For numbers, * operator  is performing multiplication
2*3

Out[3]:

6

In [4]:

## For strings, * operator is concatenating same string multiple times(mentioned number)
'Welcome to course! '*3

Out[4]:

'Welcome to course! Welcome to course! Welcome to course! '

These are built in operator overloading. Let’s see how to do custom operator overloading using sosme special functions in Python. Suppose we want to add two objects of a class and we will use ‘+’ operator it will give error. Hence, inorder to perform addition of two objects of a class will overload add method. Let’s see how to perform this -:

Custom operator overloading using special function

In [5]:

## creating a class to demonstrate the custom operator overloading
class custom_operator_overloading():
    
    ## defining the constructor function which is taking one parameter marks
    def __init__(self,marks):
        self.marks = marks
    
    ## defining get function to get the marks of the student set while calling constructor
    def get_marks(self):
        return self.marks
    
    ## defining the addition function to add two objects of this class
    ## custom operator overloading
    def __add__(self, another_student):
        return custom_operator_overloading(self.marks + another_student.marks)
        

In [6]:

## Creating first student - first object of 'custom_operator_overloading' class
s1  = custom_operator_overloading(90)
print('Marks of first student is {0} out of 100'.format(s1.get_marks()))

## Creating second student - first object of 'custom_operator_overloading' class
s2  = custom_operator_overloading(95)
print('Marks of second student is {0} out of 100'.format(s1.get_marks()))

## Adding above 2 students marks
## This is possible because we created special function __add__ in that class which is basically performs addition of two 
## object values. 
s3 = s1 + s2
print('Total of both students is {0} out of 200'.format(s3.get_marks()))

Marks of first student is 90 out of 100
Marks of second student is 90 out of 100
Total of both students is 185 out of 200

Hope you are able to understand the concept of operator overloading in Python.

Stay tuned! Keep learning Python with us.

Leave a Comment

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