The concept of operator overload revolves around the idea of using operators for purposes other than the intended ones, operator overloading can be seen in a scenario where the addition operator + can be used to add two numerical values but can also be used to concatenate strings. This is a very typical example of operator overloading. Similarly, greater than or less than operators can be used as applicators and comparators at the same time, this may also serve as an example of operator overloading.

Python is an implicit language and sometimes Python cannot differentiate between the actual purpose of operator if it is meant to be overloaded. In order to properly overload operators for certain tasks, it is recommended that custom and special functions are utilised. Custom functions are user-defined and carry a significance that is not found in the built-in functions. Special functions are the functions that are surrounded by double underscores like __init__, These depict that the function is special.

Here are some examples on how custom and special classes and functions can be used to overload certain operators.

class e1: #This defines a custom class for the purpose of oeprator overloading

   def __init__(self, x,y): #The special class does not need to be called by name, if we call init(), it will automatically relate to this one.
       self.x = x
       self.y = y

   def __add__(self,a):
       return self.x + a.x, self.y + a.y #This function is also special by the name of add which takes input directly from the class object.


o1 = e1(55,44)
o2 = e1(66,3) #Dual operator overload.

print(o1 + o2) #Prints the overloaded objects

In the afore-mentioned block of code, we have directly returned the worked values, it can be done this way or we can just simply return an object of the parent class. It is important to note that the special classes must be named properly otherwise they would not work. Here is an example of the first code with a returned object and an added str class that controls the output of the class inputs.

class e1:  # This defines a custom class for the purpose of operator overloading

   def __init__(self, x, y):  # The special class does not need to be called by name, if we call init(), it will automatically relate to this one.
       self.x = x
       self.y = y

   def __str__(self):
       return "({0},{1})".format(self.x, self.y) #format function is built-in and will control all string arguments.

   def __add__(self, a):
       x = self.x + a.x
       y = self.y + a.y  # This function is also special by the name of add which takes input directly from the class object.
       return e1(x, y) #We have returned the control variables encased in a class object


o1 = e1(55, 44)
o2 = e1(66, 3)  # Dual operator overload.

print(o1 + o2)  # Prints the overloaded objects

Overloading Comparison Operators

In a similar manner compared to the previous addition overloading method, the comparison overload method also uses objects. The only difference here is that the built in special function of __gt__  determines if the object is greater than the other and prints a true or false statement. This method also does not gain any special benefit from returning a class object although you can always do so.

class e2:  # This defines a custom class for the purpose of operator overloading

   def __init__(self, x, y):  # The special class does not need to be called by name, if we call init(), it will automatically relate to this one.
       self.x = x
       self.y = y


   def __gt__(self, other): #__gt__ is a built in special function which refers to greater than.
       a1 = self.x + self.y
       a2 = other.x + other.y
       return a1 > a2 #This function will compare each object which is overloaded with the other to check if it is greater or not.


o1 = e2(55, 44)
o2 = e2(66, 3)
o3 = e2(-2,-99) # Dual operator overload.

print(o1 > o2)
print(o2 > o3)
print(o3 > o1) # Prints the overloaded objects in the form of 'true' or 'false'




Just like a greater than comparison, a less than comparator can be used in the same format:

class e2: 

   def __init__(self, x, y): 
       self.x = x
       self.y = y


   def __lt__(self, other): #__lt__ is a built in special function which refers to less than.
   
 a1 = self.x + self.y
       a2 = other.x + other.y
       return a1 > a2


o1 = e2(55, 44)
o2 = e2(66, 3)
o3 = e2(-2,-99) # Dual operator overload.

print(o1 < o2)
print(o2 < o3)
print(o3 < o1) # Prints the overloaded objects in the form of 'true' or 'false'


Categorized in: