Coding PYTHON

Operator Overloading in Python

Operator Overloading means giving extended meaning beyond their predefined operational meaning. as an example, operator + is employed to feature two integers also as join two strings and merge two lists. it’s achievable because ‘+’ operator is overloaded by int class and stir class. you’d possibly have noticed that an equivalent built-in operator or function shows different behavior for objects of various classes, this is often called Operator Overloading.

# Python program to show use of 
# + operator for different purposes. 
  
print(1 + 2) 
  
# concatenate two strings 
print("Wiki"+"techy")  
  
# Product two numbers 
print(3 * 4) 
  
# Repeat the String 
print("Wiki"*4) 

Output:

3
Wikitechy
12
WikiWikiWikiWiki

How to overload the operators in Python?

 Consider that we’ve two objects which are a physical representation of a category (user-defined data type) and that we need to add two objects with binary ‘+’ operator it throws a mistake, because compiler don’t skills to feature two objects. So, we define a way for an operator which process is named operator overloading. we will overload all existing operators but we can’t create a replacement operator. To perform operator overloading, Python provides some special function or magic function that’s automatically invoked when it’s related to that specific operator. for instance, once we use + operator, the magic method __add__ is automatically invoked during which the operation for + operator is defined.

Overloading binary + operator in Python?

When we use an operator on user defined data types then automatically a special function or magic function related to that operator is invoked. Changing the behavior of operator is as simple as changing the behavior of method or function. You define methods in your class and operators work consistent with that behavior defined in methods. once we use + operator, the magic method __add__ is automatically invoked during which the operation for + operator is defined. There by changing this magic method’s code, we’ll give extra going to the + operator.

READ  Python Programming - Edit Distance

Code 1:

# Python Program illustrate how  
# to overload an binary + operator 
  
class A: 
    def __init__(self, a): 
        self.a = a 
  
    # adding two objects  
    def __add__(self, o): 
        return self.a + o.a  
ob1 = A(1) 
ob2 = A(2) 
ob3 = A("Wiki") 
ob4 = A("techy") 
  
print(ob1 + ob2) 
print(ob3 + ob4) 

Output:

3
Wikitechy

Code 2:

# Python Program to perform addition  
# of two complex numbers using binary  
# + operator overloading. 
  
class complex: 
    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 
  
    def __str__(self): 
        return self.a, self.b 
  
Ob1 = complex(1, 2) 
Ob2 = complex(2, 3) 
Ob3 = Ob1 + Ob2 
print(Ob3) 

Output:

(3, 5)

Overloading comparison operators in Python :

# Python program to overload equality 
# and less than operators 
  
class A: 
    def __init__(self, a): 
        self.a = a 
    def __lt__(self, other): 
        if(self.a<other.a): 
            return "ob1 is lessthan ob2"
        else: 
            return "ob2 is less than ob1"
    def __eq__(self, other): 
        if(self.a == other.a): 
            return "Both are equal"
        else: 
            return "Not equal"
                  
ob1 = A(2) 
ob2 = A(3) 
print(ob1 < ob2) 
  
ob3 = A(4) 
ob4 = A(4) 
print(ob1 == ob2) 

Output:

ob1 is lessthan ob2
Not equal

Overloading equality and less than operators :

# Python program to overload equality 
# and less than operators 
  
class A: 
    def __init__(self, a): 
        self.a = a 
    def __lt__(self, other): 
        if(self.a<other.a): 
            return "ob1 is lessthan ob2"
        else: 
            return "ob2 is less than ob1"
    def __eq__(self, other): 
        if(self.a == other.a): 
            return "Both are equal"
        else: 
            return "Not equal"
                  
ob1 = A(2) 
ob2 = A(3) 
print(ob1 < ob2) 
  
ob3 = A(4) 
ob4 = A(4) 
print(ob1 == ob2) 

Output:

ob1 is lessthan ob2
Not equal
X