In this tutorial, we’ll learn what Python operator overloading is and how to properly use it. We’ll go through multiple practical Python code examples for better understanding.
- Introduction: Python Operator Overloading
- Implementing Python Operator Overloading
- Example 1: Not Using Python Operator Overloading
- Example 2: Not Using Python Operator Overloading
- Example 3: Compare Values Of Objects Using Operator Overloading
- Important Note
Introduction: Python Operator Overloading
It specifies the process of defining the functionality of an operator depending on the operands used.
For instance, we’ve two integers and we want to add it. So for that, we’ll be using (+) operator. In Python, the functionality of this operator is specified to addition when operands are integers. It also has other functionality as well like concatenation of strings or merging lists etc.
But when we want to add two objects then it’ll give an error as addition of objects are not specified in it. That’s where Python operator overloading plays its role. We’ll overload the operator with a new functionality in which it’ll add two objects as well.
Let’s now understand all of these theories using practical Python code examples.
Implementing Python Operator Overloading
We’ll go through multiple examples in which we’ll see the result when Python operator overloading is not used and also check the result after its used.
Example 1: Not Using Python Operator Overloading
class Fun: def __init__(self,val): self.val=val val1=Fun(20) val2=Fun(30) result= val1.val + val2.val print(result)
We’ve created a simple class with init method. Then we’ve passed it an integer values and store it in two variables. After that, we’ve fetched the values of Fun class attributes using its objects and add them using (+) operator. As a result, the sum of these two values are printed.
As we can see that val1.val will fetch the attribute of class Fun. Let’s say we don’t want to fetch it like this. Instead, we just want to pass objects as operands to operator(+). See below code:
# see above block for complete code result= val1 + val2 print(result)
TypeError: unsupported operand type(s) for +: 'Fun' and 'Fun'
The output shows that no function is defined for operator(+) to add two objects. Let’s now solve it using operator overloading.
Example 2: Using Python Operator Overloading
The operator that we’re going to overload is (+). First let’s see its addition method syntax. See below:
This is how it looks. This is a special method. The method init is also special. Let’s now overload it and define the addition of objects in it. See below code:
class Fun: def __init__(self,value): self.value=value def __add__(self,val): return self.value+val.value value1=Fun(10) value2=Fun(20) result=value1+value2 print(result)
We can see that using operator(+) with object operands call the special add method. As a result, the sum is returned which we’ve printed using Python print function.
Example 3: Compare Values Of Objects Using Operator Overloading
Let’s now compare values using objects. Python operator overloading will be used to achieve it. See below code:
class FunClass: def __init__(self,a): self.a=a def __lt__(self,val): return self.a<val.a obj1=FunClass(5) obj2=FunClass(8) result=obj1<obj2 print(result)
In this case, we’ve overloaded the operator (<). So this is how we can easily specify a functionality depending on the operands used. The specific special function will be triggered depending on the operands used with the operator.
Click here for other operators method syntax.
So this is how we can properly use Python operator overloading. Feel free to share this post with your developer friends. Also, don’t hesitate to ask if you still have any questions related to Python operator overloading.
To conclude this tutorial, hope you now have an in-depth understanding of how Python operator overloading works. We’ll be very glad to receive your valuable feedback on this post.
Do visit our other articles on Python programming. Thank you for reading this post.