Mastering Python (71 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (66 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Method Overloading in Python and How it Works?

Last updated on Nov 27,2019 903 Views
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.
26 / 60 Blog from Python Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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?

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.

Method Overloading Examples

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.

Example 1:


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

Output:

Hello
Hello Edureka

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

obj.Hello()
obj.Hello('Edureka')

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.

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
else:
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

With this, we have come to the end of our article. I hope you understood what is method overloading in python and how it works.

To get in-depth knowledge on python along with its various applications, you can enroll for live Python online training by Edureka with 24/7 support and lifetime access.

Got a question for us? Please mention it in the comments section of this “Method Overloading in Python” blog and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.