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
Pythonfrom 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"Explore class relationships in the OOP Playground