Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Method Overloading in Python

Method Overloading in Python

March 14th, 2020

This tutorial helps you to learn about Method Overloading in Python. Usually, Python does not completely support Method Overloading in contrast to programming languages such as C++ which supports method overloading. Still, it is possible to overload the methods. There is a limitation that it is possible to use only the recently defined method. Mostly, more programming languages will not support method overloading.

What is Method Overloading?

The way we call a method is method overloading. In the case of python, it allows various ways to call it. Once we call a method or a function, we can denote the number of parameters. Based on the way a function is defined, it can be called with a zero, one, two or more parameters. This is called method overloading.
Let’s see some quick examples to get a detailed idea about the Method Overloading concept.

Examples of Method Overloading:

Let us create a class with one method Welcome(). We can make the first parameter of the method to ‘none’. Such a thing will enable us an advantage to call it with a parameter or without a parameter. We can create an object depending on its class and can call its method through zero or one parameter.

Example 1:

#!/usr/bin/env python
class Person:
def Welcome(friend, name=None):
if name is not None:
print('Welcome ' + name)
else:
print(' Welcome ')
# Create an instance
obj = Person()
# Call the method
obj.Welcome()
# Call the method using a parameter
obj.Hello('Karthi')

 

Output:

Welcome
Welcome Karthi

In the above example, we actually called the method Welcome() in 2 methods:

  1. Welcome()
  2. Hello(‘Karthi’)

Here, we have created a method that can be called with lesser arguments than its limitation defined. But, they are not limited to use only two variables. It is up to the user to consider whether he is going to use one, two or multiple numbers of variables.
Let’s see another example.

Example 2:

In this example, we shall use the method ‘Area’ for overloading. If in case, no argument is found, it returns as zero. If suppose we have a single argument, it calculates the area of the square(Formula: a2 ) of that argument value and returns it. Suppose, if you have two arguments, in that case, it computes the area of a rectangle (Formula: length*breadth) i.e., it will compute the product of the two arguments and returns the value.

# class
class Calculate:
# Area method
def Area(myself, a = None, b = None):
if a != None and b != None:
return a * b
elif a != None:
return a* a
else:
return 0
# object
obj = Calculate()
# No argument (Zero)
print("Value of the area:", obj.Area())
# with one argument
print("Value of the area:", obj.Area(6))
# with two arguments                                                            
print("Value of the area:", obj.Area(4, 3))

 

Output:

Value of the area: Zero
Value of the area: 36
Value of the area: 12

Suppose, if we want to take three arguments, we can use as follows:

Example 3:

# class
class Calculate:                                                        
#  Product method
def Product(x,y,z):
a=x*y*z
print(a)
Product(3,4,5)

 

Output:

60

In this case, we have used values instead of using the ‘none’ option in the beginning. We have calculated the volume of a rectangle (formula: l*b*h). So, we have taken three parameters to compute the volume of a rectangle.
With this example, we have come to the end of this tutorial. I hope this tutorial helped you to learn method overloading in python. See you all in the next tutorial.