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.