Introduction to Method Overloading in Python
Method overloading is a unique methodology offered by Python. Using this feature, a method can be defined in such a manner that it can be called in multiple ways. Every time a method is called, it depends on the user as to how to call that method, i.e., how many and what parameters to pass in the method. So, the method can be called with no or single or multiple parameters. Such a functionality offered by Python is termed as Method Overloading in Python. The feature allows the use of methods to be regulated in a user-defined manner.
Syntax:
class class_name:
def method_name(self, name = None)
# method code
# Create object instance
object_variable = class_name()
# Call the method
object_variable.method_name()
# Call the method with a single parameter
object_variable.method_name(parameter)
# Call the method with multiple parameters
object_variable.method_name(parameter1, parameter2 )
How does Method Overloading work in Python?
As described earlier, method overloading refers to the fact that a single method can be used in multiple ways by passing the variable number of parameters. E.g., we may want to return a product of specified parameters. The number of parameters can be two or three or just a single parameter.
Examples of Method Overloading in Python
Given below are the examples of Method Overloading in Python:
Example #1
Code:
# Define the class
class multiply:
# Define the method
def mult_num(self, n1 = None, n2 = None, n3 = None):
if n1 != None and n2 != None and n3 != None:
print(n1 * n2 * n3)
elif n1 != None and n2 != None:
print(n1 * n2)
else:
print(n1)
# Creating object as an instance of class
object_multiply = multiply()
# Sinlge variable method execution
object_multiply.mult_num(20)
# Two variable method execution
object_multiply.mult_num(20, 30)
# Three variable method execution
object_multiply.mult_num(20, 30, 40)
Output:
In the above program, we have class multiply. The class has a method called mult_num. This method takes three numbers as parameters. Using the if-elif-else statement, it is checked any of the numbers equals to None. When only a single number is passed as a parameter, the program prints that number. When two numbers are passed as parameters, the program returns the product of these two numbers, and when three numbers are passed as parameters, the program returns the product of the three numbers. So, we find a program being used in a manner as required by the user. Any in every situation, the method does its job correctly.
The parameters were passed as hard-coded values in the above program code, so we got the output as shown below.
Example #2
We shall modify the above program code slightly in the sense that we take the inputs from the user.
Code:
# Define the class
class multiply:
# Define the method
def mult_num(self, n1 = None, n2 = None, n3 = None):
if n1 != None and n2 != None and n3 != None:
print(n1 * n2 * n3)
elif n1 != None and n2 != None:
print(n1 * n2)
else:
print(n1)
# Creating object as an instance of class
object_multiply = multiply()
# Sinlge variable method execution
print("Single variable operation")
object_multiply.mult_num(float(input("Enter the number: ")))
# Two variable method execution
print("Two variable operation")
object_multiply.mult_num(float(input("Enter the first number: ")), float(input("Enter the second number: ")))
# Three variable method execution
print("Three variable operation")
object_multiply.mult_num(float(input("Enter the first number: ")), float(input("Enter the second number: ")), float(input("Enter the third number: ")))
Output:
We can see those input parameters are no more hard-coded. They can be passed dynamically. The default data type of the input variables is a string, so we need to convert it into a number first. As a result of this, we used the float function for type conversion. Let’s see how the program code works when executed.
Firstly, you should carry out the operation for a single variable. We passed 12.5 as the input and got the same number as the output. Please refer to the screenshot below to proceed with “Two variable operations.”
In “Two variable operations,” we specified the first number as 13.5 and the second number as 19.7, as seen in the screenshot below.
Just go through the screenshot below and see that we have the product of two decimal numbers that we passed as input parameters. The product is 265.95. The screenshot below shows the transition to “Three variable operations” after performing the two-variable operation.
Finally, we can see that the program code returns the product of three variables, as seen in the output’s bottom section.
Through the above programs, we saw how the concept of method overloading works in Python. What we do is that we create an object which we assign to the class. The class object as an operator operates over the method defined in the class. The object regulates the use of function based on the requirement.
Through the above two examples, we worked on numeric data. Now, we go through a program that demonstrates the concept of method overloading through string variable examples.
Example #3
The program code is as written below. Please go through the code and see how each of its components works.
Code:
class Name:
def name_declare(self, name1 = None, name2 = None):
if name1 is not None and name2 is not None:
print("Hello, I am " + name1 + " " + name2+".")
elif name1 is not None:
print("Hello, I am " + name1 + ".")
else:
print("Hi, How are you doing?")
object_name = Name()
# method execution by passing both name and surname.
object_name.name_declare("John", "Maurice")
# method execution by passing just name.
object_name.name_declare("Matthews")
# method execution by not passing any parameter.
object_name.name_declare()
After executing the Python program code above, the output displayed in the screenshot below.
Conclusion
Method overloading is a very crucial methodology offered by Python. The methodology proves valuable in complex situations where there is a need for condition-based utilization and execution of specific parameters. It is essential to employ the methodology appropriately to avoid incorrect utilization of any parameter.
Recommended Articles
This is a guide to Method Overloading in Python. Here we discuss the introduction, how method overloading works in Python? and examples. You may also have a look at the following articles to learn more –