What is Method Overloading in Python

In this article, we discuss What is Method Overloading in Python

What is Method Overloading in Python

What is Method Overloading in Python
What is Method Overloading in Python

What is Method Overloading in Python

Two methods cannot have the same name in Python. Method overloading in Python is a feature that allows the same operator to have different meanings. In this article, we will have a look at the method overloading feature in Python and how it is used for overloading the methods, in the following sequence:

  • What is Overloading?
  • Method Overloading in Python
  • Examples

What is Method Overloading in Python

What is Overloading?

Overloading is the ability of a function or an operator to behave in different ways based on the parameters that are passed to the function, or the operands that the operator acts on.

Some of the advantages of using overload are:

  • Overloading a method fosters reusability. For example, instead of writing multiple methods that differ only slightly, we can write one method and overload it.
  • Overloading also improves code clarity and eliminates complexity.

Overloading is a very useful concept. However, it has a number of disadvantages associated with it.

  • Overloading can create confusion when used across inheritance boundaries. When used excessively, it becomes cumbersome to manage overloaded functions.

Method Overloading in Python

In Python, you can create a method that can be called in different ways. So, you can have a method that has zero, one or more number of parameters. Depending on the method definition, we can call it with zero, one or more arguments.

Given a single method or function, the number of parameters can be specified by you. This process of calling the same method in different ways is called method overloading.


Now that you know what is method overloading in Python, let’s take an example. Here, we create a class with one method Hello(). The first parameter of this method is set to None. This will give us the option to call it with or without a parameter.

An object is also created based on the class and we will call its method using zero and one parameter.

#!/usr/bin/env python
class Person:
def Hello(self, name=None):
if name is not None:
print('Hello ' + name)
print('Hello ')
# Create instance
obj = Person()
# Call the method
# Call the method with a parameter


Hello thestackoverflow

To clarify method overloading, we can now call the method Hello() in two ways:


In the above example, we have created a method that can be called with fewer arguments than it is defined to allow. Also, it is not limited to two variables and your method can have more variables which are optional.

Now let’s take another example to understand method overloading in Python

What is Method Overloading in Python

Example 2:

In the following example, we will overload the area method. If there is no argument then it returns 0. And, If we have one argument then it returns the square of the value and assumes you are computing the area of a square. Also, if we have two arguments then it returns the product of the two values and assumes you are computing the area of a rectangle.

# class
class Compute:
# area method
def area(self, x = None, y = None):
if x != None and y != None:
return x * y
elif x != None:
return x * x
return 0
# object
obj = Compute()
# zero argument
print("Area Value:", obj.area())
# one argument
print("Area Value:", obj.area(4))
# two argument
print("Area Value:", obj.area(3, 5))

The above code will give us the following output:

Area Value: 0
Area Value: 16
Area Value: 15

we have come to the end of our article. I hope you understood what is method overloading in python

Source: Various

Also Check out Hash Tables and Hashmaps in Python

If you intreasted in Entertainment genre thebiographys.com

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *