probly.utils.switchdispatch

class probly.utils.switchdispatch(func)[source]

Bases: Generic

A switch dispatch decorator.

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

Example: ```python

@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.

__call__(arg, *args, **kwargs)[source]

Call the appropriate function based on the argument.

Parameters:
Return type:

Any

multi_register(keys: Iterable[object]) Callable[[F], F][source]
multi_register(keys: Iterable[object], f: F) F

Register a new function for the given keys.

register(key: object) Callable[[F], F][source]
register(key: object, f: F) F

Register a new function for the given key.