{"id":3742,"library":"plum-dispatch","title":"Multiple dispatch in Python","description":"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.","status":"active","version":"2.8.0","language":"en","source_language":"en","source_url":"https://github.com/beartype/plum","tags":["multiple-dispatch","type-annotations","polymorphism","decorator"],"install":[{"cmd":"pip install plum-dispatch","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Plum 2.x is powered by Beartype for instance-check-based dispatch and performance.","package":"beartype","optional":false},{"reason":"Used for rich text and terminal output.","package":"rich","optional":false},{"reason":"Provides backports of features for the `typing` module.","package":"typing-extensions","optional":false}],"imports":[{"note":"As of Plum 2.6.1, all imports should go through the top-level `plum` package directly.","wrong":"from plum.dispatch import dispatch","symbol":"dispatch","correct":"from plum import dispatch"}],"quickstart":{"code":"from numbers import Number\nfrom plum import dispatch\n\n@dispatch\ndef process(x: str):\n    return f\"Processing string: {x}\"\n\n@dispatch\ndef process(x: int):\n    return f\"Processing integer: {x}\"\n\n@dispatch\ndef process(x: Number):\n    return f\"Processing a generic number: {x}\"\n\n\nassert process(\"hello\") == \"Processing string: hello\"\nassert process(123) == \"Processing integer: 123\"\nassert process(1.0) == \"Processing a generic number: 1.0\"\n\ntry:\n    process(x=123) # This will fail due to keyword argument dispatch rule\nexcept Exception as e:\n    print(f\"Caught expected error: {type(e).__name__}: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Always pass arguments that determine dispatch as positional arguments. Avoid passing them as keyword arguments where dispatch is expected.","message":"Plum (version 2.x) relies on positional arguments for dispatch. Keyword arguments are explicitly NOT used in the decision-making for which method to call. Positional arguments without a default value must always be given positionally.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Update all import statements to use `from plum import <symbol>` instead of `from plum.submodule import <symbol>`.","message":"Import paths have changed. All imports for Plum's public API should now go directly through the `plum` package (e.g., `from plum import dispatch`). Sub-module imports are no longer recommended or supported.","severity":"breaking","affected_versions":"2.6.1+"},{"fix":"Ensure your project runs on Python 3.10 or higher to use Plum 2.x.","message":"Plum 2.x dropped support for Python 3.9.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Use parametric types judiciously and only where absolutely necessary. Profile your code if performance becomes a concern.","message":"Using parametric types (e.g., `List[int]`, `Tuple[str, int]`) for dispatch can incur a significant performance hit due to the need to check every element's type.","severity":"gotcha","affected_versions":"All"},{"fix":"If migrating from `multipledispatch`, be aware of potential behavior differences and leverage Plum's advanced features.","message":"Plum is often recommended over `multipledispatch` due to being more featureful, having better support for class inheritance, and correctly handling method precedence (choosing the most specific method).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}