Multimethod

2.0.2 · active · verified Thu Apr 09

Multimethod provides a decorator for adding multiple argument dispatching to functions in Python. It aims for simplicity and speed, utilizing type annotations to create a multimethod object that registers functions based on argument types. The library supports various type hints and advanced dispatching rules. It is currently at version 2.0.2 and has an active development and release cadence, with several releases per year.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a function `func` with multiple implementations that are dispatched based on the runtime types of its arguments. The `@multimethod` decorator is applied to each implementation.

from multimethod import multimethod

@multimethod
def func(x: int, y: float):
    return f"Int and Float: {x} + {y}"

@multimethod
def func(x: float, y: int):
    return f"Float and Int: {x} + {y}"

@multimethod
def func(x: str, y: str):
    return f"Strings: {x} {y}"

print(func(1, 2.0))
print(func(3.0, 4))
print(func("Hello", "World"))

view raw JSON →