What is Operator Overloading in PythonThe operator overloading in Python means provide extended meaning beyond their predefined operational meaning. Such as, we use the "+" operator for adding two integers as well as joining two strings or merging two lists. We can achieve this as the "+" operator is overloaded by the "int" class and "str" class. The user can notice that the same inbuilt operator or function is showing different behaviour for objects of different classes. This process is known as operator overloading. Example: Output: 46 JavaTpoint 322 X Y Z X Y Z X Y Z How to Overload the Operators in Python?Suppose the user has two objects which are the physical representation of a user-defined data type class. The user has to add two objects using the "+" operator, and it gives an error. This is because the compiler does not know how to add two objects. So, the user has to define the function for using the operator, and that process is known as "operator overloading". The user can overload all the existing operators by they cannot create any new operator. Python provides some special functions, or we can say magic functions for performing operator overloading, which is automatically invoked when it is associated with that operator. Such as, when the user uses the "+" operator, the magic function __add__ will automatically invoke in the command where the "+" operator will be defined. How to Perform Binary "+" Operator in Python:When the user uses the operator on the user-defined data types of class, then a magic function that is associated with the operator will be invoked automatically. The process of changing the behaviour of the operator is as simple as the behaviour of the function or method defined. The user define methods or functions in the class and the operator works according to that behaviour defined in the functions. When the user uses the "+" operator, it will change the code of a magic function, and the user has an extra meaning of the "+" operator. Program 1: Simply adding two objects.Python program for simply using the overloading operator for adding two objects. Example: Output: Please enter the value: 23 Please enter the value: 21 : 44 Please enter the value: Java Please enter the value: Tpoint : JavaTpoint Program 2: defining Overloading operator in another objectPython program for defining the overloading operator inside another object. Example: Output: (44, 34) Program 3: Overloading comparison operators in PythonPython program for overloading comparison operators. Example: Output: Case 1: Please enter the value: 23 Please enter the value: 12 The object_1 is greater than object_2 Case 2: Please enter the value: 20 Please enter the value: 31 The object_2 is greater than object_1 Program 4: Overloading equality and less than operatorsPython Program for overloading equality and less than operators: Example: Output: Case 1: Please enter the value: 12 Please enter the value: 23 : object_1 is less than object_2 Please enter the value: 2 Please enter the value: 2 : Both the objects are equal Case 2: Please enter the value: 26 Please enter the value: 3 : object_2 is less than object_1 Please enter the value: 2 Please enter the value: 5 : Objects are not equal Python magic functions used for operator overloading:Binary Operators:
Comparison Operators:
Assignment Operators:
Unary Operator:
ConclusionIn this tutorial, we have discussed overloading operators in Python and how to use them to perform various operators. Next Topicnsetools in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India