Python Method Overloading: Flexible Function Definitions

thumb_up 1  ·  sell Python method overloading, Overloading functions in Python, Creating flexible function definitions in Python

Method overloading is an important feature of object-oriented programming. Java, C++, C# languages support method overloading, but in Python it is not possible to perform method overloading.

When you have a class with method of one name defined more than one but with different argument types and/or return type, it is a case of method overloading. Python doesn't support this mechanism as the following code shows −

Example

 
class example: def add(self, a, b): x = a+b return x def add(self, a, b, c): x = a+b+c return x obj = example() print (obj.add(10,20,30)) print (obj.add(10,20))

Output

The first call to add() method with three arguments is successful. However, calling add() method with two arguments as defined in the class fails.

60
Traceback (most recent call last):
 File "C:\Users\user\example.py", line 12, in <module>
  print (obj.add(10,20))
         ^^^^^^^^^^^^^^
TypeError: example.add() missing 1 required positional argument: 'c'

The output tells you that Python considers only the latest definition of add() method, discarding the earlier definitions.

To simulate method overloading, we can use a workaround by defining default value to method arguments as None, so that it can be used with one, two or three arguments.

Example

 
class example: def add(self, a = None, b = None, c = None): x=0 if a !=None and b != None and c != None: x = a+b+c elif a !=None and b != None and c == None: x = a+b return x obj = example() print (obj.add(10,20,30)) print (obj.add(10,20))

It will produce the following output −

60
30

With this workaround, we are able to incorporate method overloading in Python class.

Python's standard library doesn't have any other provision for implementing method overloading. However, we can use dispatch function from a third party module named MultipleDispatch for this purpose.

First, you need to install the Multipledispatch module.

pip install multipledispatch

This module has a @dispatch decorator. It takes the number of arguments to be passed to the method to be overloaded. Define multiple copies of add() method with @dispatch decorator as below −

Example

from multipledispatch import dispatch class example: @dispatch(int, int) def add(self, a, b): x = a+b return x @dispatch(int, int, int) def add(self, a, b, c): x = a+b+c return x obj = example() print (obj.add(10,20,30)) print (obj.add(10,20))

Output

60
30



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back