Operator Overloading is changing the purpose of usage of the operators as the type of operands is changed.
Let’s take the example of ‘+’ operator, then it will add two numbers and it will also concatenate the strings. So, as the type of operands gets changed the operators change its behavior during evaluation.
Similarly goes for ‘‘ if we are multiplying numbers using ‘‘ then it will perform mathematics multiplication and the same operator(‘*’) when applying with string and number then the output will change.
Checking these concepts with code snippets -:
Operator Overloading in inbuilt operators
How ‘+’ and ‘*’ operator is overloading.
These are built in operator overloading. Let’s see how to do custom operator overloading using some special functions in Python. Suppose we want to add two objects of a class and we will use ‘+’ operator it will give an error. Hence, in order to perform addition of two objects of a class will overload add method. Let’s see how to perform this -:
Custom operator overloading using a special function
Hope you are able to understand the concept of operator overloading in Python.
Stay tuned! Keep learning Python with us.