What is method overloading in Python with example?

In Python, method overloading refers to the ability to define multiple methods with the same name but different parameters or argument types. This allows you to perform different operations based on the number of arguments or their types.

Python does not support explicit method overloading by default. However, you can achieve similar behavior using some techniques. One common approach is to use default parameter values and conditional statements within a single method to handle different cases.

Here’s an example that demonstrates method overloading in Python:

class Calculator:
    def add(self, num1, num2):
        return num1 + num2
    
    def add(self, num1, num2, num3):
        return num1 + num2 + num3

# Creating an instance of the Calculator class
calc = Calculator()

# Calling the add() method with two arguments
result1 = calc.add(2, 3)
print("Result 1:", result1)  # Output: 5

# Calling the add() method with three arguments
result2 = calc.add(2, 3, 4)
print("Result 2:", result2)  # Output: 9
Code language: Python (python)

In this example, the Calculator class defines two add() methods with different numbers of parameters. The first add() method takes two parameters and returns their sum. The second add() method takes three parameters and returns the sum of all three numbers.

By calling the add() method with different numbers of arguments, you can see that the appropriate method is invoked based on the number of arguments provided.

It’s important to note that this approach doesn’t enforce strict method overloading like in other languages, as the latest definition of the method overrides any previous definitions with the same name. Therefore, only the second add() method will be accessible in the above example.

Example for method overloading in Python

Python does not have built-in support for method overloading, you can achieve similar functionality by using a third-party library called multipledispatch. This library allows you to define multiple functions with the same name but different argument types.

You can install the multipledispatch library using pip:

pip install multipledispatchCode language: Python (python)

Here’s an example of how to use multipledispatch for method overloading in Python:

from multipledispatch import dispatch

class Calculator:
    @dispatch(int, int)
    def add(self, num1, num2):
        return num1 + num2
    
    @dispatch(int, int, int)
    def add(self, num1, num2, num3):
        return num1 + num2 + num3

# Creating an instance of the Calculator class
calc = Calculator()

# Calling the add() method with two arguments
result1 = calc.add(2, 3)
print("Result 1:", result1)  # Output: 5

# Calling the add() method with three arguments
result2 = calc.add(2, 3, 4)
print("Result 2:", result2)  # Output: 9
Code language: Python (python)

In this example, the multipledispatch library is used to decorate the methods with the @dispatch decorator. Each method is defined with a specific combination of argument types. When you call the add() method, multipledispatch determines the appropriate method to invoke based on the types of the arguments.

Using multipledispatch allows you to achieve method overloading-like behavior in Python.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment