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 form or type of the attributes. For achieving the overloading process, there are two key means in a 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 frequent instance is the adding up operator ‘+’, where it can be used for the usual addition and also for 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 process of concatenation 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 different lists with it, and a third list gets generated by concatenating the two lists. The newly generated list is compared with an existing output reference for a match. If a match is taking place, then the newly generated list gets printed in the console. Here the overloading process is achieved by means of the ‘+’ operator, which is responsible for concatenating the two lists into a single list. 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 ‘ * ‘. This operator not only responsible for multiplying two given numbers but also has the capability to perform the repetition of 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: As in the above program, here also two completely different lists are used for generating the third list. But different from the above concept here, the overloading process is carried out by means of the ‘*’ operator, which is accountable for repeating the contents of a given list a specified number of times and then generate 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
Even the comparison operators like > and < exhibit very similar operator overloading outcomes. They display a great difference in handling 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 the given example, two strings are compared to determine which one is greater using the greater than and less than operator.
4. Equal to Operator Overloaded
The equal operator (==) is used for comparing two different 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: In the above-given program, two different lists are matched. The ” == ” operator is used for matching the lists. Here one list consists of lower case values, whereas the other list consists of upper case values. The equal to operator matches both the 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. The ” == ” operator is again used for matching the lists. Here one list consists of lower case values, whereas the other list also holds lower case values only. The equal to operator matches both the lists; if the match exists, then a console message has been printed here.
Advantages of Operator Overloading
- Reusability of code is greatly increased
- Code clarity is increased
- The complexity involved in the code is vastly condensed
Recommended Articles
This is a guide to Operator Overloading in Python. Here we discuss the Introduction and the top 4 operators of overloading in python and various examples. You may also look at the following articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses