Skip to content

Archive

Design Patterns

1 articles
Python 02 Sep 2026 7 min read

Single Dispatch in Python: Extensible Type-Based Behavior

A function that accepts several kinds of input often begins with a few isinstance() checks. That approach is straightforward when the cases are small and local. As the number of supported types grows, however, one function can become a long decision tree that mixes unrelated implementations. Python’s functools.singledispatch offers another design. It turns one function into a generic function whose implementation is selected from the runtime type of its first argument. Type-specific behavior can then be registered separately while callers keep using one public function.