Multiple dispatch

1.0.0 · active · verified Thu Apr 09

Multiple dispatch (also known as multimethods) is a programming concept that allows a function or method to be dynamically dispatched based on the runtime types of more than one of its arguments. The `multipledispatch` library provides an efficient Python implementation of this concept, performing static analysis to avoid conflicts and offering optional namespace support. The current stable version is 1.0.0, released in June 2023, with an infrequent release cadence.

Warnings

Install

Imports

Quickstart

Defines a function `add` that behaves differently based on the types of its arguments, demonstrating basic multiple dispatch.

from multipledispatch import dispatch

@dispatch(int, int)
def add(x, y):
    return x + y

@dispatch(object, object)
def add(x, y):
    return f"{x} + {y}"

print(add(1, 2)) # Expected: 3
print(add(1, 'hello')) # Expected: '1 + hello'

view raw JSON →