Skip to content
UPEEC

Method Overloading Patterns

Python has no signature-based overloading; use default arguments, *args or functools.singledispatch instead.

Intermediate
Comments 0
from functools import singledispatch

@singledispatch
def describe(x): return "something"

@describe.register
def _(x: int): return "an integer"

@describe.register
def _(x: list): return f"a list of {len(x)}"

typing.overload only helps type checkers; at runtime the last definition wins.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.