{"id":3276,"library":"singledispatch","title":"singledispatch","description":"The `singledispatch` library provides a standalone implementation of the `functools.singledispatch` decorator, which allows for generic functions to have different behaviors based on the type of their first argument. Originally a backport for older Python versions, the PyPI package currently targets Python 3.9+ and is actively maintained with regular releases, as evidenced by recent version updates.","status":"active","version":"4.1.2","language":"en","source_language":"en","source_url":"https://github.com/jaraco/singledispatch","tags":["type dispatch","polymorphism","functools","decorator","generic functions"],"install":[{"cmd":"pip install singledispatch","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"While `functools.singledispatch` is built into Python 3.4+, this package provides a standalone implementation. If you install and use the `singledispatch` package, you should import from it directly to ensure you are using its version.","wrong":"from functools import singledispatch","symbol":"singledispatch","correct":"from singledispatch import singledispatch"},{"note":"`singledispatchmethod` was added to `functools` in Python 3.8. If targeting Python versions prior to 3.8 (though the `singledispatch` package itself now requires Python 3.9+), or if using this package's specific implementation, import from `singledispatch` directly.","wrong":"from functools import singledispatchmethod","symbol":"singledispatchmethod","correct":"from singledispatch import singledispatchmethod"}],"quickstart":{"code":"from singledispatch import singledispatch\n\n@singledispatch\ndef process_data(arg):\n    \"\"\"Default implementation for process_data\"\"\"\n    return f\"Processing generic data: {arg}\"\n\n@process_data.register(int)\ndef _(arg):\n    \"\"\"Process integer data\"\"\"\n    return f\"Processing integer: {arg * 2}\"\n\n@process_data.register(list)\ndef _(arg):\n    \"\"\"Process list data\"\"\"\n    return f\"Processing list: {', '.join(map(str, arg))}\"\n\nprint(process_data(\"hello\"))\nprint(process_data(10))\nprint(process_data([1, 2, 3]))\nprint(process_data(3.14))","lang":"python","description":"This example demonstrates how to create a single-dispatch generic function using the `@singledispatch` decorator. It defines a default implementation and then registers specific implementations for `int` and `list` types. When called, the appropriate function is dispatched based on the type of the first argument."},"warnings":[{"fix":"For new projects on Python 3.4+, consider using `from functools import singledispatch`. If you specifically need this package, ensure you import from `singledispatch`.","message":"Potential confusion with `functools.singledispatch` which is part of Python's standard library since Python 3.4. If you are using Python 3.4 or newer, `functools.singledispatch` is generally preferred unless you specifically need the `singledispatch` package's independent implementation or its associated utilities.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade your Python environment to 3.9+ or pin `singledispatch` to a version older than 4.0.0 (e.g., `~=3.7`) if you need to support Python 3.8.","message":"Version 4.0.0 dropped support for Python 3.8, now requiring Python 3.9 or later. This is a significant breaking change for environments still using Python 3.8.","severity":"breaking","affected_versions":"4.0.0 and later"},{"fix":"If you require dispatching on multiple arguments or keyword arguments, you will need to implement a custom solution or use a third-party library designed for multiple dispatch.","message":"The `singledispatch` decorator, by its nature, dispatches solely based on the *type of the first argument*. It does not support multiple dispatch (dispatching on multiple arguments) or dispatching based on keyword arguments.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}