Functional Functions

functools.singledispatch()

Create a generic function that chooses an implementation from the runtime type of its first argument. Register specialized functions by class; subclasses and registered ABCs can affect dispatch.

Formal Syntax & Structure

python syntaxpython
functools.singledispatch(func)

Parameters & Attributes

NameTypeDescription
funccallableCallable applied by the operation.

Return Value

callable

The callable result described above.

Working Example

Example Snippetpython
import functools@functools.singledispatchdef show(x): return "other"@show.register(int)def _(x): return "integer"print((show(3),show("hi")))
Python Reference