Operator overloading refers to the ability of a built-in operator to behave differently according to the different operands we use with it.
For example, the operator ‘+’ is used to:
- Add two integers.
- Concatenate two strings.
- Merge two lists.
#Concatenate two strings print("Codes"+"Dope") #Add two numbers print(9+6) #Multiply two numbers print(6*5) #Repeation of a string print("Welcome"*3)
Advantages of Operator Overloading:
- It provides reusability, instead of writing multiple methods that differ slightly we can simply write one method and overload it.
- It also improves code clarity and eliminates complexity.
- It makes code concise and simple to understand.
Why operator overloading?
Consider two objects of a user-defined data type, now if we try to multiply both with an operator
* the compiler will throw an error because the compiler doesn’t know how to multiply the operands.
For example, let's take a case where we have defined our own Coordinates class with two attributes x and y. Now, objects of this class would represent different points and using
+ directly on these points (objects) would give us an error. Let's look at the example to understand this.
class Coordinates: def __init__(self, x, y): self.x = x self.y = y p1 = Coordinates(1, 2) p2 = Coordinates(2, 3) print(p1+p2)
Traceback (most recent call last): File "<string>", line 9, in <module>
TypeError: unsupported operand type(s) for +: 'Coordinates' and 'Coordinates'.
Here, the compiler is unable to understand what
To resolve this problem we can define a method to overload the
+ operator to change its functionality as per our needs.
How to perform operator overloading?
*operator, the magic method
__mul__is automatically invoked in which the operation for
*operator is already defined.
+' operator, we will use the
class Coordinates: def __init__(self, a,b): self.a = a self.b=b # adding two objects def __add__(self, o): a=self.a+o.a b=self.b+o.b s3=Coordinates(a,b) return s3 p1=Coordinates(30,40) p2=Coordinates(70,80) s3=p1+p2 print(s3.a) #sum of the x coordinates print(s3.b) #sum of the y coordinates
Here we have defined the
__add()__ method and passed the Coordinates object and got the sum of the coordinates as the result.
This is how we can define the magic methods and can also change their behavior according to our needs.
In python whenever we use ‘+’ operator, python directly call the method that is associated to it i.e. __add()__ method.
Python arithmetic on two complex numbers :
class complex: def __init__(self, real, imag): self.real = real self.imag = imag def __add__(self, other): return complex(self.real+other.real, self.imag+other.imag) def __sub__(self, other): return complex(self.real-other.real, self.imag-other.imag) def __str__(self): return str(round(self.real,2))+('+' if self.imag>=0 else '-')+str(round(abs(self.imag),2))+'i' print("Enter real and imaginary part of complex No - 1(separeated by space)") A = complex(*map(float,input().split())) #the numbers are split by the space between them and passed to the function. * is used to pass multiple values at the same time. print("Enter real and imaginary part of complex No - 2(separeated by space)" ) B = complex(*map(float,input().split())) print("Addition: " + str(A+B)) print("Subtraction: " + str(A-B))
Enter real and imaginary part of complex No - 1(separeated by space)
Enter real and imaginary part of complex No - 2(separeated by space)
Here, we have entered two complex numbers, containing their real and imaginary parts.
Now, these numbers are passed to the complex function, defined by us.
After writing (A+B) python will automatically call the
__add__() function because of (
+) symbol. The same will happen for (
-) symbol, the
__sub__() function will be called.
Our function will now add and subtract, both real and imaginary parts of the numbers together, simultaneously.
Now, the results will be passed to the
__str()__ function, which will concatenate both the real and imaginary parts together.
Python magic function for binary operators overloading
Bitwise Left Shift
Bitwise Right Shift
Python magic function for comparison operators overloading
Python doesn't limit operator overloading to arithmetic operators. We can overload other operators like comparison, assignment operators as well.
For Example: If we want to overload greater than (
>) and equal to operator (
class Comparison: def __init__(self, a): self.a = a def __gt__(self, o): if(self.a>o.a): return "m1 is greater than m2" else: return "m2 is greater than m1" def __eq__(self, o): if(self.a == o.a): return "Both are equal" else: return "Not equal" m1 = Comparison(3) m2 = Comparison(2) print(m1>m2) m3=Comparison(5) m4=Comparison(6) print(m3==m4)
m1 is greater than m2
Here we are passing two numbers into the function.
Case 1- After writing
m1>m2, python will automatically call the
__gt__() function, because of the (
Case 2- Similarly,
__eq__() will be called automatically after writing
Once the conditions are checked, we will get our result.
Table of the magic methods for comparison operators:
Less Than Equals
Greater Than Equals
Python magic function for assignment operators overloading
Assignment operators perform a particular operation on the value of a variable and assign the result to that variable.
Floor Division Assignment
Right Shift Assignment
Left Shift Assignment
Now move ahead to the next chapter.
Learn the Python Programming Language and strengthen your grip on the subject.