skillfed

singledispatch

Backport functools.singledispatch to older Pythons.

singledispatch v4.1.2 2.0M downloads/30d#3,372 on PyPI5
Permissive license MIT Active released

What it is and what it does

singledispatch is a backport of Python's functools.singledispatch decorator, which implements single-dispatch generic functions—a lightweight form of function overloading based on the type of the first argument. When you decorate a function with @singledispatch, you can register specialized implementations for specific types using the .register() method; at runtime, the dispatcher automatically calls the appropriate implementation based on the argument's type, falling back to the base implementation for unregistered types.

This is useful when you want to write cleaner, more maintainable code that handles multiple types without nested if-elif chains or isinstance checks scattered throughout. The backport allows you to use this functionality consistently across Python versions, and it also includes singledispatchmethod for class methods. Since the standard library version is built-in to Python 3.4+, this package is most valuable for codebases that need to support older Python versions or for projects that want to stay synchronized with the latest backport enhancements.

Use it for:

  • Implement type-specific behavior in a library without explicit type checking or if-elif chains.
  • Create a plugin system where different types are handled by different registered handlers.
  • Build serializers or formatters that need different logic for different types.
  • Simplify mathematical or data-processing functions that operate on multiple numeric types.
  • Define class methods that dispatch on argument type in object-oriented designs.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides single-dispatch generic functions—a decorator-based mechanism to define functions that behave differently based on the type of their first argument—backported from Python 3.4's functools module for use on older Python versions.

Yes, if you need single-dispatch functionality on Python versions before 3.4 or want the latest backport enhancements. No, if you're on Python 3.4+ and can use functools.singledispatch directly from the standard library. The package is stable, well-maintained, has no known vulnerabilities, and carries no license friction.

Install

singledispatch on PyPI

pip

pip install singledispatch

uv

uv add singledispatch

poetry

poetry add singledispatch

Installing singledispatch

Before you install

Low install friction; pure Python wheel with no runtime dependencies. Actively maintained by a CPython core team member with recent commits as of 2026-04-13.

License in practice

MIT license permits unrestricted use, modification, and distribution in both open-source and proprietary projects with minimal restrictions.

Quickstart

pip install singledispatch

from singledispatch import singledispatch

@singledispatch
def process(arg):
    print(f"Default: {arg}")

@process.register(int)
def _(arg):
    print(f"Integer: {arg}")

process(arg)

Requires Python 3.9 or later.

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.9)
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 457 days since the last release
Last repo commit
First released
Downloads 2,001,134/month — #3,372 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: singledispatch-4.1.2-py3-none-any.whl

Keywords: single, dispatch, generic, functions, singledispatch, genericfunctions, decorator, backport

Development Status :: 5 - Production/StableIntended Audience :: DevelopersProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: Only

Tags

single dispatch decoratorgeneric functions by typefunctools singledispatch backportmethod overloading pythontype-based function dispatch
generic-functionsdecoratorbackport

More Software Development packages