{"id":5477,"library":"simplegeneric","title":"Simple generic functions","description":"The `simplegeneric` module enables the creation of simple single-dispatch generic functions in Python, akin to built-in functions like `len()` or `iter()`. It achieves this through internal lookup tables rather than special method names. The library, currently at version 0.8.1, was last updated in 2012, signifying a mature but no longer actively developed project that remains functional for its intended purpose. It boasts no external runtime dependencies beyond a minimum Python version.","status":"maintenance","version":"0.8.1","language":"python","source_language":"en","source_url":"https://github.com/mindw/simplegeneric","tags":["generic functions","single-dispatch","decorators","generic programming","dispatch"],"install":[{"cmd":"pip install simplegeneric","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"generic","correct":"from simplegeneric import generic"}],"quickstart":{"code":"from simplegeneric import generic\n\n@generic\ndef move(item, target):\n    \"\"\"Default implementation\"\"\"\n    return f\"Default move: {item} to {target}\"\n\n@move.when_type(int)\ndef move_int(item, target):\n    return f\"Moving integer {item} to AD {target}\"\n\n@move.when_type(str)\ndef move_str(item, target):\n    return f\"Moving string '{item}' to {target}. All your {target} are belong to us.\"\n\nclass Zig: pass\nzig_instance = Zig()\n\n@move.when_object(zig_instance)\ndef move_zig(item, target):\n    return f\"Special move for Zig object: {item} to {target}. For great justice!\"\n\nassert move(2101, \"war\") == \"Moving integer 2101 to AD war\"\nassert move(\"gentlemen\", \"base\") == \"Moving string 'gentlemen' to base. All your base are belong to us.\"\nassert move(zig_instance, \"doing\") == \"Special move for Zig object: <object of type Zig> to doing. For great justice!\"\nassert move(27.0, 56.2) == \"Default move: 27.0 to 56.2\"","lang":"python","description":"Define a generic function using `@generic` and specialize implementations based on the type or identity of the first argument using `@func.when_type` or `@func.when_object`."},"warnings":[{"fix":"Ensure you are using `simplegeneric` version 0.8.1 or later when installing with Python 3. For versions <0.8.1, manual installation or Python 2.x might be required for `setup.py` functionality.","message":"Prior to version 0.8.1, the `setup.py` script was not fully compatible with Python 3, potentially causing issues during installation or packaging on Python 3 environments. The core module was Python 3 compatible from version 0.8.","severity":"breaking","affected_versions":"<0.8.1"},{"fix":"Always pass the dispatching argument as a positional argument.","message":"Dispatching always occurs on the first argument, and this argument must be passed positionally when calling the generic function. Keyword arguments for the first parameter will not trigger dispatch.","severity":"gotcha","affected_versions":"All"},{"fix":"Document the expected arguments and their types for all specialized methods within the docstring of the default `@generic` function.","message":"Standard documentation tools (like `help()`) will only display the signature of the default function. The specialized method signatures are not visible, requiring their documentation to be explicitly stated in the default function's docstring.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure all optional arguments are included with their desired defaults in the signature of every `@func.when_type` or `@func.when_object` decorated function.","message":"If a generic function has optional arguments, these arguments (including their defaults) must be explicitly duplicated on every specialized method. Omitting them will result in a `TypeError` if they are not provided in a call.","severity":"gotcha","affected_versions":"All"},{"fix":"Evaluate `functools.singledispatch` for modern Python development. `simplegeneric` is a mature but unmaintained library that predates this standard library feature.","message":"For new projects on Python 3.4+, consider using the built-in `functools.singledispatch` decorator. It provides similar single-dispatch functionality and is actively maintained as part of Python's standard library, offering better integration and type hinting support.","severity":"gotcha","affected_versions":"All (especially Python 3.4+)"}],"env_vars":null,"search_vec":"'0.8.1':46 '2012':51 'achiev':30 'activ':58 'akin':19 'beyond':74 'boast':69 'built':22 'built-in':21 'creation':9 'current':43 'decor':84 'depend':73 'develop':59 'dispatch':14,83,87 'enabl':7 'extern':71 'function':3,16,24,63,80 'generic':2,15,79,85 'intend':66 'intern':33 'iter':28 'last':48 'len':26 'librari':42 'like':25 'longer':57 'lookup':34 'matur':54 'method':39 'minimum':76 'modul':6 'name':40 'program':86 'project':60 'purpos':67 'python':18,77 'rather':36 'remain':62 'runtim':72 'signifi':52 'simpl':1,11 'simplegener':5 'singl':13,82 'single-dispatch':12,81 'special':38 'tabl':35 'updat':49 'version':45,78","created_at":"2026-04-14T01:35:32.163531+00:00","updated_at":"2026-04-16T21:45:22.816388+00:00","problems":[{"fix":"Install the package using pip: `pip install simplegeneric`","cause":"The `simplegeneric` package is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'simplegeneric'"},{"fix":"Apply the `@simplegeneric.generic` decorator directly above the function definition: \n```python\nfrom simplegeneric import generic\n\n@generic\ndef my_generic_function(arg):\n    pass\n\n@my_generic_function.register(int)\ndef _(arg):\n    return f'Integer: {arg}'\n```","cause":"The function intended to be a generic dispatch function was not correctly decorated with `@simplegeneric.generic`, so it lacks the `.register` method.","error":"AttributeError: 'function' object has no attribute 'register'"},{"fix":"Import `generic` directly from the `simplegeneric` package: `from simplegeneric import generic`","cause":"The `generic` function is a direct export from the `simplegeneric` package's `__init__.py`, not located within a submodule named `simplegeneric.generic`.","error":"ModuleNotFoundError: No module named 'simplegeneric.generic'"},{"fix":"Provide a valid Python type (e.g., `int`, `str`, `list`) or a tuple of types to the `.register()` method: \n```python\nfrom simplegeneric import generic\n\n@generic\ndef process(item):\n    return f'Default processing: {item}'\n\n@process.register(str) # Correct: str is a type\ndef _(item):\n    return f'Processing string: {item.upper()}'\n\n@process.register((int, float)) # Correct: tuple of types\ndef _(item):\n    return f'Processing number: {item * 2}'\n```","cause":"The `.register()` method was called with an argument that is not a Python type (e.g., `int`, `str`) or a tuple of types.","error":"TypeError: isinstance() arg 2 must be a type or tuple of types"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.8.1","cli_name":"","cli_version":null,"type":"library","homepage":"http://cheeseshop.python.org/pypi/simplegeneric","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/simplegeneric/","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-30","next_check":"2026-07-28","install_tag":null}}