Python 3.8+functoolsfunction
functools.singledispatchmethod
generic functions
A method decorator with single dispatch on the type of the first argument (after self).
Syntax
@functools.singledispatchmethod
Added in Python 3.8; it dispatches on the type of the first argument after self, normally taken from that argument's annotation. Registered implementations are conventionally named _ because the name is irrelevant — only register matters — and when stacking with classmethod or staticmethod, singledispatchmethod has to be the outermost decorator or its register attribute is unreachable.
Examples
from functools import singledispatchmethod
class C:
@singledispatchmethod
def f(self, x): return 'other'
@f.register
def _(self, x: int): return 'int'
print(C().f(5)) # → int
See also
Open in the full glossarydocs.python.orgReport an inaccuracy