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
- Python | Operator.countOf
- Operator Functions in Python | Set 2
- Ternary Operator in Python
- Operator Functions in Python | Set 1
- Ternary operator vs Null coalescing operator in PHP
- C# | Constructor Overloading
- C# | Overloading of Indexers
- set operator= in C++ STL
- 'AND' vs '&&' as operator in PHP
- What is the difference between the | and || or operator in php?
- Double not (!!) operator in PHP
- unordered_multiset operator = in C++ STL
- unordered_set 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 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.
Improved By : bestharadhakrishna