Operator Overloading in Python

In Python, operator overloading happens with built-in classes. The operator works based on the application requirement. In Python, one single operator has different meanings according to the context. This feature of operators in Python is known as Operator overloading. For example, the + operator is used to add numbers and at the same time, it is also used for concatenating strings. This + operator is also used to merge lists. A single + operator works for different purposes according to the requirements. This is an example of operator overloading.

Working procedure of overloading the operators in Python:

Suppose there are two objects that represent a class and we want to add two objects. So, the + operator is used to add two objects. But an error will be thrown by this + operator as the Python compiler does not know how to add two numbers. To resolve that problem a method or function is needed to define the operator. Except for new operators, all the existing operators can be overloaded. But the programmer does not need to do anything explicitly. Some magic functions or inbuilt functions are provided by the Python language that is invoked automatically when the operators are called. For a particular operator, a particular method or function is invoked by Python.  Some lists of in-built functions are given below for which operation which methods are given.

Operator Sample Operation Method
+ a + b Addition operation is done by using the method a. __ add __ ( b )
- a - b Subtraction operation is done by using the method a. __ sub __ ( b )
* a * b Multiplication operation is done by using the method a. __ mul __ ( b )
** a ** b Power operation is done by using the method a. __ pow __ ( b )
/ a / b Division operation is done by using the method a. __ truediv __ ( b )
// a // b Floor Division operation is done by using the method a. __ floordiv __ ( b )
% a % b Remainder operation is done by using the method a. __ mod __ ( b )
<< a << b Bitwise Left Shift operation is done by using the method a. __ lshift __ ( b )
>> a >> b Bitwise Right Shift operation is done by using the method a. __ rshift __ ( b )
& a & b Bitwise AND operation is done by using the method a. __ and  __ ( b )  
| a | b Bitwise OR operation is done by using the method a. __ or __ ( b )
^ a ^ b Bitwise XOR operation is done by using the method a. __ xor __ ( b )
~ a ~ b Bitwise NOT operation is done by using the method a. __ invert __ ( b )  
< a < b Less Than operation is done by using the method a. __ lt __ ( b )
<= a <= b Less Than or Equal to operation is done by using the method a. __ le __ ( b )
= = a = = b Equal to operation is done by using the method a. __ eq __ ( b )
! = a ! = b Not Equal To operation is done by using the method a. __ ne __ ( b )
> a > b Greater Than operation is done by using the method a. __ gt __ ( b )
>= a >= b Greater Than or Equal To operation is done by using the method : a. __ ge __ ( b )  

Overloading binary + operator in Python:

The + operator is overloaded by using the __ add __ ( ) method.

A sample code is given for method overloading,

class Student: # creation of Student class
     def __ init __ ( self, v1 ):
         self. v1 = v1
     def __ add __ ( self, v2 ): # magic method is used to add two objects
         return self. v1 + v2. v1

s1 = Student (5) # creation of object
s2 = Student (10)
s3 = Student ("Jack")
s4 = Student ("Smith")

print(s1+s2) # it prints 15
print(s3+s4) # it prints Tack Smith