View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

A Complete Guide To Method Overloading in Python (With examples)

By Rohit Sharma

Updated on Dec 21, 2023 | 5 min read | 6.1k views

Share:

Overloading is the ability of a function, method, or operator to work differently when you pass different parameters to the same. Method overloading or Function overloading in Python have commonly used terms. Some of the main advantages of overloading is that you can use one method in multiple ways, which helps you keep your code cleaner and removes complexity when working with a team.

What is Method Overloading?

In Object-Oriented Programming, Method Overloading is used in scenarios where, for a specific object, a particular method can be called in more than one way according to the project requirement.

Examples of Method Overloading in Python are discussed in detail later in the article.

 What is Method Overriding?

Method Overriding in Python is similar to Method Overloading except for that method overriding occurs between a subclass and a superclass. It has the same parameters as to when the methods are called. Yet, they behave differently due to some of the functionality being overridden from the superclass.

Example of Method Overriding

class X:

    def method1(self):
        print(‘I’m the first feature of class X’)
    def method2(self):
        print(‘I’m the second feature of class X’)
class Y(X):
    def method1(self):
        print(‘I’m the modified first feature of class X in class Y’)   
    def method3(self):
        print(‘I’m a feature of class Y’)
obj = Y()
obj.method1()
Output:

I’m the modified first feature of class X in class Y

method1 was overridden by class Y.

Learn Data Science Courses online at upGrad

Method Overloading in Python

The problem with method overloading in Python is that Python does not support it by default. However, there are workarounds to do the same.

The Problem

Let us consider the following code:

def add(a, b):
s = a + b
print(s)
def add(a, b, c):
s = a + b + c
print(s)
# add(8, 9) shows error
add(8, 9, 2)

At first glance, the code looks good, but when you try to execute it with two arguments, Python will show you an error because in Python, when you have more than one method of the same name but a different number of arguments, only the latest defined method can be used. 

There are two different ways we can overcome this problem of method overloading in Python.

1: Using the same methods differ according to the data type of the arguments

We can see an argument to know the data type, along with *args that allows passing a variable number of arguments to a method in Python. We can then use if statements to control how the method behaves according to the input.

CODE: 

def add(dt, *args):
if dt ==’int’:
s = 0
if dt ==’str’:
s = ”
for x in args:
s = s + x
print(s)
add(‘int’, 11, 9)
add(“str”, “Hello,”, “How are you?”)
Output:

20

Hello, How are you?

This was the first workaround to implement method overloading in Python.

2: Using Multiple Dispatch Decorator (More efficient way)

Multiple Dispatch Decorator is less of a workaround and works exactly like it is supposed to. You can install it using pip3.

pip3 install multiple dispatches

CODE:
from multiple dispatch import dispatch
@dispatch(int,int) # for 2 integer arguments
def add(n1,n2):
    s = n1 + n2
    print(s) 
@dispatch(int,int,int) # for 3 integer arguments
def add(n1,n2,n3):
    s = n1 + n2 + n3
    print(s)
@dispatch(float,float,float) # for float arguments
def add(n1,n2,n3):
    s = n1 + n2 + n3
    print(s  
add(5,2)
add(6,1,4) 
add(3.4,1.2,5.6) 
Output:
7
11
10.2

When executing, the dispatcher makes a new object that stores different implementations of the method and decides the method to select depending on the type and number of arguments passed while calling the method. This way method of overloading in Python is more efficient.

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

 

 

Conclusions

If you wish to enter the field of Data Science, Python is a good first step to take. To dive deep and study the topic further, you can check out advanced online certifications courses like the Executive Programme in Data Science by IIIT-Bangalore in association with upGrad. This program covers the important aspects of the subject and provides a lot of additional benefits like job assistance, 1:1 Mentorship, online support, live lectures, and optional additional modules for enthusiasts who want to upskill further.

Check out All Python tutorial concepts Explained with Examples.

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months
View Program

Placement Assistance

Certification8-8.5 Months
View Program

Frequently Asked Questions (FAQs)

1. What is the difference between a function and a method in Python?

2. How do I choose between Python and R?

3. How long does it take to master Data Science?

Rohit Sharma

697 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

View Program
Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

View Program
upGrad Logo

Certification

3 Months

View Program