{"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":"en","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,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}