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.
pip install simplegenericVerified import paths — ran on the pinned version, not inferred.
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`.
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.
Always pass the dispatching argument as a positional argument.
Document the expected arguments and their types for all specialized methods within the docstring of the default `@generic` function.
Ensure all optional arguments are included with their desired defaults in the signature of every `@func.when_type` or `@func.when_object` decorated function.
Evaluate `functools.singledispatch` for modern Python development. `simplegeneric` is a mature but unmaintained library that predates this standard library feature.
Install the package using pip: `pip install simplegeneric`
Apply the `@simplegeneric.generic` decorator directly above the function definition:
```python
from simplegeneric import generic
@generic
def my_generic_function(arg):
pass
@my_generic_function.register(int)
def _(arg):
return f'Integer: {arg}'
```Import `generic` directly from the `simplegeneric` package: `from simplegeneric import generic`
Provide a valid Python type (e.g., `int`, `str`, `list`) or a tuple of types to the `.register()` method:
```python
from simplegeneric import generic
@generic
def process(item):
return f'Default processing: {item}'
@process.register(str) # Correct: str is a type
def _(item):
return f'Processing string: {item.upper()}'
@process.register((int, float)) # Correct: tuple of types
def _(item):
return f'Processing number: {item * 2}'
```No dependency data recorded yet.