Skip to main content

Plum: Multiple Dispatch in Python

DOI CI Coverage Status Latest Docs Code style: black

Everybody likes multiple dispatch, just like everybody likes plums.

The design philosophy of Plum is to provide an implementation of multiple dispatch that is Pythonic, yet close to how Julia does it. See here for a comparison between Plum, multipledispatch, and multimethod.

Note: Plum 2 is now powered by Beartype! If you notice any issues with the new release, please open an issue.

Installation

Plum requires Python 3.8 or higher.

pip install plum-dispatch

Documentation

See here.

What's This?

Plum brings your type annotations to life:

from numbers import Number

from plum import dispatch


@dispatch
def f(x: str):
    return "This is a string!"


@dispatch
def f(x: int):
    return "This is an integer!"


@dispatch
def f(x: Number):
    return "This is a general number, but I don't know which type."
>>> f("1")
'This is a string!'

>>> f(1)
'This is an integer!'

>>> f(1.0)
'This is a number, but I don't know which type.'

>>> f(object())
NotFoundLookupError: For function `f`, `(<object object at 0x7fb528458190>,)` could not be resolved.

This also works for multiple arguments, enabling some neat design patterns:

from numbers import Number, Real, Rational

from plum import dispatch


@dispatch
def multiply(x: Number, y: Number):
    return "Performing fallback implementation of multiplication..."


@dispatch
def multiply(x: Real, y: Real):
    return "Performing specialised implementation for reals..."


@dispatch
def multiply(x: Rational, y: Rational):
    return "Performing specialised implementation for rationals..."
>>> multiply(1, 1)
'Performing specialised implementation for rationals...'

>>> multiply(1.0, 1.0)
'Performing specialised implementation for reals...'

>>> multiply(1j, 1j)
'Performing fallback implementation of multiplication...'

>>> multiply(1, 1.0)  # For mixed types, it automatically chooses the right optimisation!
'Performing specialised implementation for reals...'

Metadata

Release files for fenbux-plum-dispatch 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fenbux-plum-dispatch 0.0.2
File Size Uploaded Provenance Actions
fenbux_plum_dispatch-0.0.2.tar.gz 26.0 kB Without provenance Details for fenbux_plum_dispatch-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fenbux-plum-dispatch 0.0.2
File Interpreter ABI Platform Actions
fenbux_plum_dispatch-0.0.2-py3-none-any.whl Python 3 none any Details for fenbux_plum_dispatch-0.0.2-py3-none-any.whl

Total release size: 58.4 kB

Release files / fenbux_plum_dispatch-0.0.2.tar.gz

Download URL fenbux_plum_dispatch-0.0.2.tar.gz
Size 26.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8f353dac1f6457c1e934f2a6e5ba2ca5497189a39a80f32cf2c6e142f89556d1
BLAKE2b-256 checksum
How to use checksums
6894fdb65c477c2b49e24eb927e1bed8a256ba7c0521f23f667ca8db57051a75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Release files / fenbux_plum_dispatch-0.0.2-py3-none-any.whl

Download URL fenbux_plum_dispatch-0.0.2-py3-none-any.whl
Size 32.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65558aa9004fecf4ebaf81046fda74fc6926a3c5f2061e7a93eed296e625d135
BLAKE2b-256 checksum
How to use checksums
7781ec5729fc6ee5c3ecd076bb5ed214fb9529739beed4b3462afef67f2a01a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.13

Security for fenbux-plum-dispatch 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page