Python Programming – Python Operator Overloading

In the previous chapter, we learn the fundamentals of OOPS, i.e., the creation of user-defined data type classes and objects in Python. In this chapter, we will learn one of the significant features of object-oriented. programming structures (OOPS) that is operator overloading. As implied from the name, operator overloading means assigning special meaning to the existing operator to perform some intended task. In other words, the same operator exhibiting different meanings as per the situation is called operator overloading. For example, the “+’ operator is used to add two numbers, the same can be used for merging two lists and concatenating two strings. It indicates that the same ‘+’ operator can be used to perform different tasks based on the context in which it is being used.

Similarly, two-class objects can also be added by using the concept of operator overloading by using the similar syntax that is used for adding two integer numbers. In the following sections, the concept of operator overloading is discussed in detail.

Python Programming – Overloading Operator in Python

Alike, the ‘+’ operator overloading, the subtraction operator can also be overloaded. The programming code for the same is given in Code 12.2. The code is apparently similar to the addition operator overloading for adding up two objects cl and c2. The only difference is that instead of the ‘+’ operator, the operator is used. The built-in Python function used for overloading ‘-‘ is sub( ), which performs the subtraction of two objects and provides the appropriate result. Alike, addition and subtraction operators, other arithmetic operators can also be overloaded. The list of all arithmetic operators with the built-in overloading functions is given in Table 12.1.

Code: 12.2. Illustration of overloading operator in Python

#illustration of subtraction’-‘operator overloading

class complex:
‘A complex number class’
def__init__(self, real, imag):
self.real = real
self.imag = imag

def__sub__(self, obj):
real = self.real – obj .real
imag = self.imag – obj.imag
retum(complex(real, imag))

def display(self):
print(self.real,”+ i”,self.imag)

cl=complex(5, 8)
c2=complex(6, 7)
cl.display()
c2.display()
c3=cl-c2
c3.display()

Output

5 + i 8
6 + i 7
-1 + i 1

Operator Expression Built-in function
Addition (+) cl + c2 C1 add (c2)
Subtraction (-) cl -c2 C1 sub (c2)
Multiplication (*) cl * c2 C1 pow (c2)
Power (**) cl ** c2 C1 pow (c2)
Division (/) cl / c2 C1 truediv (c2)
Floor Division (//) cl // c2 C1 floordiv (c2)
Remainder (modulo) (%) cl % c2 C1 __mod__ (c2)

Summary

In this chapter, we have learned one of the most exciting features of OOPS, which is known as operator overloading. Operator overloading refers to make the same operator perform different tasks such as the *+’ operator which is used to add two numbers can be used to add two objects as well. All the arithmetic operators can be overloaded, where the programming illustration to overload the ‘+’ operator is given. Apart from that the bitwise operators and relational operators can also be overloaded. The Python language provides built-in functions for overloading operators. In Python language, to overload, an operator is rather simple as that of C++ and Java by using the built-in functions for overloading. The list of all of these functions is given for help supporting the operator overloading.

Python Tutorial

Leave a Reply

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