Plum-dispatch is a Python library that provides a powerful and Pythonic implementation of multiple dispatch, allowing functions to behave differently based on the types of multiple arguments. Its design philosophy is inspired by Julia's approach to multiple dispatch, and version 2.x is powered by the `beartype` library for enhanced performance. The library is actively maintained, with its current version being 2.8.0, and has seen consistent updates.
pip install plum-dispatchVerified import paths — ran on the pinned version, not inferred.
Demonstrates defining multiple versions of a function using the `@dispatch` decorator, where the appropriate implementation is chosen based on the runtime type of the arguments. It also highlights the restriction on using positional arguments for dispatch.
Always pass arguments that determine dispatch as positional arguments. Avoid passing them as keyword arguments where dispatch is expected.
Update all import statements to use `from plum import <symbol>` instead of `from plum.submodule import <symbol>`.
Ensure your project runs on Python 3.10 or higher to use Plum 2.x.
Use parametric types judiciously and only where absolutely necessary. Profile your code if performance becomes a concern.
If migrating from `multipledispatch`, be aware of potential behavior differences and leverage Plum's advanced features.
Ensure that a `@dispatch` method is defined for the argument types being passed (or a suitable superclass). For arguments that `plum-dispatch` needs to dispatch on, ensure they are passed positionally, not as keyword arguments.
Use string forward references for type hints (e.g., `other: "Real"`) to defer the evaluation of the type hint until the class is fully defined.
Define a type promotion rule for the involved types using `plum.add_promotion_rule(type_from_a, type_from_b, type_to_promote_to)`.
Review the class method definition and how `plum-dispatch` is applied. Ensure arguments are correctly passed according to the dispatched method's signature and that any custom `__call__` implementations align with `plum-dispatch`'s argument handling.