Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables


Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators


Python - If Else statement


Python - While LoopPython - For Loop


Python - NumbersPython - Number Conversion


Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method


Python - ListPython - List Methods


Python - Tuple


Python - SetPython - Set Methods


Python - DictionaryPython - Dictionary Methods


Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables


Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module


Python - Read input from keyboard


Python - File Handling

Exception Handling

Python - Exception Handling


Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Method Overloading


python logo

In this tutorial we will learn about method overloading in Python.

We learned about method overriding in the Python - Method Overriding tutorial. Feel free to check that out.

What is method overloading?

In Python we can create a method that can be called in different ways.

So, we can have a method that has zero, one or more number of parameters and depending on the method definition we can call it with zero, one or more arguments.

This is method overloading in Python.

Point to note!

Method overloading in Python is achieved by using one method with different number of arguments.

In other programming languages like Java we have more than one method definition for the same method name to achieve method overloading in Java.

Example of method overloading

In the following Python program we are overloading the area method.

If there is no argument then it returns 0.

If we have one argument then it returns square of the value and assumes you are computing the area of a square.

And 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:", obj.area())

# one argument
print("Area:", obj.area(2))

# two argument
print("Area:", obj.area(4, 5))

The above code will give us the following output.

Area: 0
Area: 4
Area: 20