generate_multimethod

uarray.generate_multimethod(argument_extractor: Callable[[...], Tuple[uarray._backend.Dispatchable, ...]], argument_replacer: Callable[[Tuple, Dict, Tuple], Tuple[Tuple, Dict]], domain: str, default: Optional[Callable] = None)[source]

Generates a multimethod.

Parameters
  • argument_extractor (ArgumentExtractorType) – A callable which extracts the dispatchable arguments. Extracted arguments should be marked by the Dispatchable class. It has the same signature as the desired multimethod.

  • argument_replacer (ArgumentReplacerType) – A callable with the signature (args, kwargs, dispatchables), which should also return an (args, kwargs) pair with the dispatchables replaced inside the args/kwargs.

  • domain (str) – A string value indicating the domain of this multimethod.

  • default (Optional[Callable], optional) – The default implementation of this multimethod, where None (the default) specifies there is no default implementation.

Examples

In this example, a is to be dispatched over, so we return it, while marking it as an int. The trailing comma is needed because the args have to be returned as an iterable.

>>> def override_me(a, b):
...   return Dispatchable(a, int),

Next, we define the argument replacer that replaces the dispatchables inside args/kwargs with the supplied ones.

>>> def override_replacer(args, kwargs, dispatchables):
...     return (dispatchables[0], args[1]), {}

Next, we define the multimethod.

>>> overridden_me = generate_multimethod(
...     override_me, override_replacer, "ua_examples"
... )

Notice that there’s no default implementation, unless you supply one.

>>> overridden_me(1, "a")
Traceback (most recent call last):
    ...
uarray.BackendNotImplementedError: ...
>>> overridden_me2 = generate_multimethod(
...     override_me, override_replacer, "ua_examples", default=lambda x, y: (x, y)
... )
>>> overridden_me2(1, "a")
(1, 'a')

See also

uarray

See the module documentation for how to override the method by creating backends.