Introduction to Operator Overloading in Python
The Python operator overloading process means more than one element will be holding the same name (methods, operators, etc.), but the difference exists in the attributes of the entities. The dissimilarity in attributes can be because of dissimilarity in the count of the attributes used or even the differentiation in the form or type of the attributes. To achieve the overloading process, there are two key means in Python programming language; they are listed below,
- Function Overloading
- Operator Overloading
Top 4 Operators of Overloading in Python
Here we discuss the different operator overloading in Python:
1. Plus Operator Overloaded
This involves an extended interpretation of an operator more than its original purpose. The most common use of the addition operator ‘+’ is for concatenating and combining two different strings. As mentioned on top, the plus symbol’s practice in dissimilar forms is the largest classic example of the operator-level overloading process. Especially the ability of the binary operator to perform the concatenation process on the pair of the string is especially special.
Example
Code:
# Using len() function without method overloading
Lower_Case=['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' ]
Upper_Case=['A','B','C','D','E','F', 'G','H','I' ,'J','K','L', 'M','N','O','P','Q','R','S' ,'T','U','V','W','X','Y','Z' ]
Expected_Output = ['a','b','c','d','e','f','g','h' ,'i','j','k', 'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B', 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', 'T','U','V','W','X','Y','Z']
print(' Type of List1 : ', type(Lower_Case))
print(' Type of list2 : ', type(Upper_Case))
print(' Total entries count in list1 : ',len(Lower_Case))
print(' Total entries count in list2 : ',len(Upper_Case))
List_Concat = Lower_Case + Upper_Case
if Expected_Output == List_Concat:
print("")
print(" LIST AFTER CONCATENATION ")
print(List_Concat)
print(' Total count ofconcatenated list',len(Upper_Case))
else:
print(" List concatenation did not match ")
Output:
Explanation: The program consists of two lists; to check for a match, we combine the two lists into a third list and compare it to an existing reference list. The console will display the newly generated list if it matches. To combine the two lists, use the ‘+’ operator to overload the process. So here, the plus operator behaves differently from its actual behavior of adding two values.
2. Multiplication Operator Overloaded
Another similar example of a binary operator involved in the overloading process can be noticed in the usage of’ * ‘. Not only does this operator multiply two numbers, but it is also capable of repeating strings and lists.
Example
Code:
# Using len() function without method overloading
Lower_Case=['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' ]
Upper_Case=['A','B','C','D','E','F', 'G','H','I' ,'J','K','L', 'M','N','O','P','Q','R','S' ,'T','U','V','W','X','Y','Z' ]
Expected_Output=['a','b','c','d','e','f','g','h' ,'i','j','k', 'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B', 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S', 'T','U','V','W','X','Y','Z']
print(' Type of entries in List1 : ', type(Lower_Case))
print(' Type of entries in list2 : ', type(Upper_Case))
print(' Total Alphabets count in list1 : ', len(Lower_Case))
print(' Total Alphabets count in list2 : ', len(Upper_Case))
List_Concat = 2*Upper_Case + 2*Lower_Case
if Expected_Output == List_Concat:
print(" ")
print(" LIST AFTER CONCATENATION ")
print(List_Concat)
print(' Total count ofconcatenated list',len(Upper_Case))
else :
print(" List concatenation did not match: ")
print(" List generated is printed below for reference: ")
print(List_Concat)
Output:
Explanation: Like the program above, we create the third list using two distinct lists. In this case, we use the ‘*’ operator to overload the process. The ‘*’ operator repeats the contents of a list a certain number of times and generates them into a single list. So here, the multiplication operator behaves differently from its actual behavior of multiplying two values.
3. Greater than and Less than Operator Overloaded
The comparison operators like > and < exhibit similar operator overloading outcomes. They display a great difference in handling the usual numbers and strings.
Example
Code:
String1 = "Hello"
String2 = "World"
Out1 = String1 > String2
Out2 = String1 < String2
print("string1 greater than string2 : ",Out1)
print("string1 less than string2 : ",Out2)
Output:
Explanation: In this example, we compare two strings to determine which one is greater using the greater than and less than operators.
4. Equal to Operator Overloaded
You can use the equal operator (==) to compare two distinct lists.
Example #1
Code:
Lower_Case=['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' ]
Upper_Case=['A','B','C','D','E','F', 'G','H','I' ,'J','K','L', 'M','N','O','P','Q','R','S' ,'T','U','V','W','X','Y','Z' ]
print(' List1 entries : ', type(Lower_Case))
print(' List2 entries : ', type(Upper_Case))
print(' Total Alphabets count in list1 : ', len(Lower_Case))
print(' Total Alphabets count in list2 : ', len(Upper_Case))
if Lower_Case == Upper_Case:
print(" The lists are matching ")
else :
print(" The lists are not matching ")
Output:
Explanation: The program above matches two separate lists. The” == “operator is used for matching the lists. Here one list consists of lowercase values, whereas the other list consists of uppercase values. The equal to operator matches both lists; if the match exists, a console message has been printed here.
Example #2
Code:
Lower_Case=['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' ]
Upper_Case=['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z' ]
print(' List1 entries : ', type(Lower_Case))
print(' List2 entries : ', type(Upper_Case))
print(' Total Alphabets count in list1 : ', len(Lower_Case))
print(' Total Alphabets count in list2 : ', len(Upper_Case))
if Lower_Case == Upper_Case:
print(" The lists are matching ")
else :
print(" The lists are not matching ")
Output:
Explanation: This is the opposite condition of the above-given example1; here again, two different lists are used for matching in the given program. You match lists again using the “==” operator. One list contains lowercase values, whereas the other only holds lowercase values. The equal to operator matches both lists; if the match exists, a console message has been printed here.
Advantages of Operator Overloading
- The reusability of code is greatly increased
- Code clarity is increased
- The complexity involved in the code is vastly condensed
Recommended Articles
We hope that this EDUCBA information on “Operator Overloading in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.