switchdispatch

class probly.utils.switchdispatch(func: Callable[Concatenate[T, In], Out])[source]

Bases: Generic[T, In, Out]

A switch dispatch decorator.

Similar to functools.singledispatch, but dispatches based on equality rather than type.

Example

>>> @switchdispatch
>>> def func(x):
>>>     return "default"
>>>
>>> @func.register(1)
>>> def _(x):
>>>     return "one"
>>>
>>> @func.register(2)
>>> def _(x):
>>>     return "two"
>>>
>>> print(func(1))  # Output: "one"
>>> print(func(2))  # Output: "two"
>>> print(func(3))  # Output: "default"

Initialize the switchdispatch with the default function.

__call__(arg: T, *args: In.args, **kwargs: In.kwargs) Out[source]

Call the appropriate function based on the argument.

multi_register(keys: Iterable[object]) _FunctionRegistration[Callable[Concatenate[T, In], Out]][source]
multi_register(keys: Iterable[object], f: F) F

Register a function for the given keys, returning it unchanged.

register(key: T) _FunctionRegistration[Callable[Concatenate[T, In], Out]][source]
register(key: T, f: F) F

Register a function for the given key, returning it unchanged.