Skip to content

Description:

  • A decorator for adding multiple argument dispatching to functions. The decorator creates a multimethod object as needed, and registers the function with its annotations.

Resources:

Map a signature to a function.

from multimethod import multimethod
import operator
classic_div = multimethod(operator.truediv)
classic_div[int, int] = operator.floordiv
classic_div

Introspect type annotations and use the name to find existing multimethods.

from multimethod import multimethod
import itertools
from collections.abc import Iterable, Sequence
@multimethod
def chunks(values: Iterable, size):
it = iter(values)
return iter(lambda: list(itertools.islice(it, size)), [])
@multimethod
def chunks(values: Sequence, size):
for index in range(0, len(values), size):
yield values[index : index + size]
list(chunks(iter('abcde'), 3))

ganymede is made by Eugo