Skip to content

Archive

Extensibility

1 articles
Python 04 Sep 2026 9 min read

Use functools.singledispatch for Type-Based Extension Points in Python

A function often starts with one input type and later grows branches for several related types. The first version may be straightforward: def render(value): if isinstance(value, str): ... elif isinstance(value, list): ... elif isinstance(value, dict): ... As the number of supported types grows, this function becomes the place where every extension must be added. The branches mix dispatch logic with the behavior for each type, and independently maintained modules cannot add support without editing the central function.