Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.
3 GeeksFor 12 GeeksGeeksGeeksGeeks
How to overload the operators in Python?
Consider that we have two objects which are a physical representation of a class (user-defined data type) and we have to add two objects with binary ‘+’ operator it throws an error, because compiler don’t know how to add two objects. So we define a method for an operator and that process is called operator overloading. We can overload all existing operators but we can’t create a new operator. To perform operator overloading, Python provides some special function or magic function that is automatically invoked when it is associated with that particular operator. For example, when we use + operator, the magic method __add__ is automatically invoked in 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 associated with 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 according to that behavior defined in methods. When we use + operator, the magic method __add__ is automatically invoked in which the operation for + operator is defined. There by changing this magic method’s code, we can give extra meaning to the + operator.
Overloading comparison operators in Python :
ob2 is greater than ob1
Overloading equality and less than operators :
ob1 is lessthan ob2 Not equal
Python magic methods or special functions for operator overloading
Comparison Operators :
Assignment Operators :
Unary Operators :
- Concatenate two strings using Operator Overloading in Python
- Operator Overloading in Ruby
- Benefits of Double Division Operator over Single Division Operator in Python
- Ternary operator vs Null coalescing operator in PHP
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Python | Method Overloading
- Difference between Method Overloading and Method Overriding in Python
- C# | Constructor Overloading
- C# | Overloading of Indexers
- Overloading in PHP
- Function overloading with singledispatch-functools
- Method Overloading In Ruby
- Operator Functions in Python | Set 2
- Ternary Operator in Python
- Operator Functions in Python | Set 1
- Python | Operator.countOf
- Walrus Operator in Python 3.8
- Python | List comprehension vs * operator
- Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
- What is a modulo operator (%) in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.