What is Operator Overloading in Python
The 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.
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.
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 object
Python program for defining the overloading operator inside another object.
Program 3: Overloading comparison operators in Python
Python program for overloading comparison operators.
Please enter the value: 23 Please enter the value: 12 The object_1 is greater than object_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 operators
Python Program for overloading equality and less than operators:
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
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:
In this tutorial, we have discussed overloading operators in Python and how to use them to perform various operators.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week