{"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":"python","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,"search_vec":"'3.9':43 'activ':46 'allow':14 'argument':29 'backport':32 'base':22 'behavior':21 'current':40 'decor':12,61 'differ':20 'dispatch':58 'evidenc':52 'first':28 'function':17,63 'functool':60 'functools.singledispatch':11 'generic':16,62 'implement':8 'librari':4 'maintain':47 'older':34 'origin':30 'packag':39 'polymorph':59 'provid':5 'pypi':38 'python':35,42 'recent':54 'regular':49 'releas':50 'singledispatch':1,3 'standalon':7 'target':41 'type':25,57 'updat':56 'version':36,55","created_at":"2026-04-11T09:28:34.475144+00:00","updated_at":"2026-04-16T21:46:48.977227+00:00","problems":[{"fix":"pip install singledispatch","cause":"The `singledispatch` package has not been installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'singledispatch'"},{"fix":"from singledispatch import singledispatch","cause":"The `singledispatch` module was imported directly, and then the module object was incorrectly used as a decorator.","error":"TypeError: 'module' object is not callable"},{"fix":"from singledispatch import singledispatch\n\n@singledispatch\ndef my_function(arg):\n    # Default implementation\n    pass\n\n@my_function.register(int)\ndef _(arg: int):\n    return arg + 1","cause":"The `.register()` method was called on a regular Python function that had not been decorated with `@singledispatch`.","error":"AttributeError: 'function' object has no attribute 'register'"},{"fix":"from singledispatch import singledispatch\n\n@singledispatch\ndef my_function(arg):\n    # This is a callable function\n    pass","cause":"The `singledispatch` decorator or function was applied to, or passed, a non-callable object.","error":"TypeError: singledispatch expects a callable as its first argument"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"4.1.2","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/jaraco/singledispatch","docs":null,"changelog":null,"pypi":"https://pypi.org/project/singledispatch/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}