Browse OOP reference
Types and contracts

What is method overloading?

On this page

Overloading gives one name several callable signatures. Java resolves overloads at compile time using the available types and its applicability rules, while an instance override can still be chosen at runtime. In Python, typing.overload describes alternatives to a type checker and requires a separate runtime implementation in ordinary modules.

Worked example

Python
from typing import overload

@overload
def render(value: bytes) -> str: ...
@overload
def render(value: int) -> str: ...

def render(value):
    if isinstance(value, bytes):
        return value.hex()
    return str(value)

assert render(b"A") == "41"
assert render(41) == "41"

Python typing reference

Explore class relationships in the OOP Playground

What are protocols?What is a method?What is dynamic dispatch?

All OOP questions

Search ObjectFAQ