probly.utils.switchdispatch¶
- class probly.utils.switchdispatch(func)[source]¶
Bases:
GenericA switch dispatch decorator.
Similar to functools.singledispatch, but dispatches based on equality rather than type.
@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”
- Parameters:
func (C)
- __init__(func)[source]¶
Initialize the switchdispatch with the default function.
- Parameters:
func (C)
- Return type:
None
Methods
__init__(func)Initialize the switchdispatch with the default function.
multi_register()Register a new function for the given keys.
register()Register a new function for the given key.