Multimethod
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:
- [GitHub] https://github.com/coady/multimethod
- [Examples] https://coady.github.io/multimethod/user-guide/examples.html
Example 1
Section titled “Example 1”Map a signature to a function.
from multimethod import multimethodimport operator
classic_div = multimethod(operator.truediv)classic_div[int, int] = operator.floordivclassic_divExample 2
Section titled “Example 2”Introspect type annotations and use the name to find existing multimethods.
from multimethod import multimethodimport itertoolsfrom collections.abc import Iterable, Sequence
@multimethoddef chunks(values: Iterable, size): it = iter(values) return iter(lambda: list(itertools.islice(it, size)), [])
@multimethoddef 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