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 :
- Python | Method Overloading
- Difference between == and is operator in Python
- Operator Functions in Python | Set 2
- Ternary Operator in Python
- Python | Operator.countOf
- Operator Functions in Python | Set 1
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Ternary operator vs Null coalescing operator in PHP
- Overloading in PHP
- C# | Overloading of Indexers
- C# | Constructor Overloading
- set operator= in C++ STL
- 'AND' vs '&&' as operator in PHP
- unordered_set operator= in C++ STL
- unordered_multiset operator = in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : bestharadhakrishna