Python
function overloading
programming
coding
software development

Python function overloading

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python does not support function overloading in the classic C++ or Java sense, where several functions with the same name coexist and the runtime chooses one based on parameter types or counts. If you define the same function name twice in Python, the later definition simply replaces the earlier one. That does not mean you are stuck. It means Python solves the same design problem in different ways.

What Happens If You “Overload” a Function Name

Consider this code:

python
1def greet(name):
2    return f"Hello, {name}"
3
4def greet(name, title):
5    return f"Hello, {title} {name}"
6
7print(greet("Ada", "Dr."))

This works, but only because the second definition overwrote the first one. The original one-argument version no longer exists.

That is the first thing to understand: Python does not keep multiple live definitions for the same function name in a module namespace.

Use Default Arguments for Simple Variants

The most Pythonic replacement for many overloads is a single function with optional parameters:

python
1def greet(name, title=None):
2    if title is None:
3        return f"Hello, {name}"
4    return f"Hello, {title} {name}"
5
6print(greet("Ada"))
7print(greet("Ada", "Dr."))

This is usually the best solution when the behavior differences are small and the parameters are closely related.

Use *args and **kwargs for Flexible Signatures

If the function genuinely needs to accept different argument counts, you can inspect variable arguments manually:

python
1def area(*args):
2    if len(args) == 1:
3        radius = args[0]
4        return 3.14159 * radius * radius
5    if len(args) == 2:
6        width, height = args
7        return width * height
8    raise TypeError("area expects 1 or 2 arguments")
9
10print(area(3))
11print(area(4, 5))

This simulates overloaded behavior, but you should use it carefully. It is easy to make the function hard to read or hard to validate.

Use Type Checks When Behavior Depends on Input Type

Sometimes the same operation name really does vary by argument type:

python
1def stringify(value):
2    if isinstance(value, int):
3        return f"int:{value}"
4    if isinstance(value, list):
5        return ",".join(map(str, value))
6    return str(value)
7
8print(stringify(7))
9print(stringify([1, 2, 3]))

This is fine when the branching is simple, but if the dispatch logic grows, there are better tools.

Use functools.singledispatch for Type-Based Dispatch

Python includes a standard-library tool for one-argument type dispatch: singledispatch.

python
1from functools import singledispatch
2
3@singledispatch
4def describe(value):
5    return f"generic:{value}"
6
7@describe.register
8def _(value: int):
9    return f"integer:{value}"
10
11@describe.register
12def _(value: list):
13    return f"list with {len(value)} items"
14
15print(describe(10))
16print(describe([1, 2, 3]))
17print(describe("hi"))

This is closer to true type-based overloading, but it dispatches on the first argument only. That makes it useful, but not a universal replacement for all overload models from statically typed languages.

typing.overload Is for Type Checkers, Not Runtime Dispatch

Python also has typing.overload, but it often confuses people because it helps static type checkers and editors, not runtime behavior.

python
1from typing import overload
2
3@overload
4def parse(value: int) -> str: ...
5
6@overload
7def parse(value: str) -> int: ...
8
9def parse(value):
10    if isinstance(value, int):
11        return str(value)
12    if isinstance(value, str):
13        return int(value)
14    raise TypeError("unsupported type")

At runtime there is still only one real parse function. The overload declarations are for tools such as mypy or IDEs.

Choosing the Right Technique

Use default arguments when:

  • the variations are small
  • the signature is still clear
  • one function naturally owns the behavior

Use *args or **kwargs when:

  • the input shapes genuinely vary
  • the branching is still understandable

Use singledispatch when:

  • the primary difference is the type of the first argument
  • you want a cleaner extensible dispatch model

Use typing.overload when:

  • you want better static typing and editor hints
  • you understand that it does not create runtime dispatch by itself

Common Pitfalls

The biggest pitfall is assuming repeated def statements with the same name create overloaded functions. In Python, the last definition wins.

Another common issue is overusing *args and type checks until one function becomes a pile of dispatch logic. At that point, separate functions or classes may be cleaner.

People also often misunderstand typing.overload and expect it to change runtime behavior. It does not.

Finally, if your branching logic is complex enough to resemble a dispatch engine, step back and ask whether polymorphism, separate named functions, or a class hierarchy would express the design better.

Summary

  • Python does not support traditional runtime function overloading by repeated definitions.
  • Default arguments and flexible parameter handling solve many “overload” use cases idiomatically.
  • 'functools.singledispatch is the standard library tool for type-based dispatch on one argument.'
  • 'typing.overload helps static analysis, not runtime dispatch.'
  • In Python, the best replacement for overloading is usually the simplest clear design, not trying to imitate another language exactly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.