probly.utils.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: Any, **kwargs: Any) Any[source]

Call the appropriate function based on the argument.

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

Register a new function for the given keys.

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

Register a new function for the given key.