Skip to main content

Multiple dispatch in Python

Project description

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.10 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 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: `f(<object object at 0x7fd3b01cd330>)` could not be resolved.

Closest candidates are the following:
    f(x: str)
        <function f at 0x7fd400644ee0> @ /<ipython-input-2-c9f6cdbea9f3>:6
    f(x: int)
        <function f at 0x7fd3a0235ca0> @ /<ipython-input-2-c9f6cdbea9f3>:11
    f(x: numbers.Number)
        <function f at 0x7fd3a0235d30> @ /<ipython-input-2-c9f6cdbea9f3>:16

[!IMPORTANT] Dispatch, as implemented by Plum, is based on the positional arguments to a function. Keyword arguments are not used in the decision making for which method to call. In particular, this means that positional arguments without a default value must always be given as positional arguments!

Example:

from plum import dispatch

@dispatch
def f(x: int):
   return x

>>> f(1)        # OK
1

>> try: f(x=1)  # Not OK
... except Exception as e: print(f"{type(e).__name__}: {e}")
NotFoundLookupError: `f()` 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...'

Projects Using Plum

The following projects are using Plum to do multiple dispatch! Would you like to add your project here? Please feel free to open a PR to add it to the list!

  • Coordinax implements coordinates in JAX.
  • fasttransform provides the main building block of data pipelines in fastai.
  • GPAR is an implementation of the Gaussian Process Autoregressive Model.
  • GPCM is an implementation of various Gaussian Process Convolution Models.
  • Galax does galactic and gravitational dynamics.
  • Geometric Kernels implements kernels on non-Euclidean spaces, such as Riemannian manifolds, graphs, and meshes.
  • LAB uses Plum to provide backend-agnostic linear algebra (something that works with PyTorch/TF/JAX/etc).
  • MLKernels implements standard kernels.
  • MMEval is a unified evaluation library for multiple machine learning libraries.
  • Matrix extends LAB and implements structured matrix types, such as low-rank matrices and Kronecker products.
  • NetKet, a library for machine learning with JAX/Flax targeted at quantum physics, uses Plum extensively to pick the right, efficient implementation for a large combination of objects that interact.
  • NeuralProcesses is a framework for composing Neural Processes.
  • OILMM is an implementation of the Orthogonal Linear Mixing Model.
  • PySAGES is a suite for advanced general ensemble simulations.
  • Quax implements multiple dispatch over abstract array types in JAX.
  • Unxt implements unitful quantities in JAX.
  • Varz uses Plum to provide backend-agnostic tools for non-linear optimisation.

See the docs for a comparison of Plum to other implementations of multiple dispatch.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plum-2.8.0.tar.gz (241.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

plum-2.8.0-py3-none-any.whl (44.5 kB view details)

Uploaded Python 3

plum-2.8.0-cp313-cp313-win_amd64.whl (146.9 kB view details)

Uploaded CPython 3.13Windows x86-64

plum-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (193.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

plum-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl (165.1 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

plum-2.8.0-cp312-cp312-win_amd64.whl (146.8 kB view details)

Uploaded CPython 3.12Windows x86-64

plum-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (194.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

plum-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl (165.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

plum-2.8.0-cp311-cp311-win_amd64.whl (146.5 kB view details)

Uploaded CPython 3.11Windows x86-64

plum-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (192.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

plum-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl (162.1 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

plum-2.8.0-cp310-cp310-win_amd64.whl (146.8 kB view details)

Uploaded CPython 3.10Windows x86-64

plum-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (194.7 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

plum-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl (164.4 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file plum-2.8.0.tar.gz.

File metadata

  • Download URL: plum-2.8.0.tar.gz
  • Upload date:
  • Size: 241.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0.tar.gz
Algorithm Hash digest
SHA256 9e0d2ac224b50a0a33a77d06fadf1c83fd5101a5a6891c616b24e1a653382dd9
MD5 7a45acdd546e668355d51fd92058fed6
BLAKE2b-256 0ffe0294ff528f147d8026eb2f27cb7c149f6aef708785d14d7c3bf6390b6b59

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0.tar.gz:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-py3-none-any.whl.

File metadata

  • Download URL: plum-2.8.0-py3-none-any.whl
  • Upload date:
  • Size: 44.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce9a2f9e3eb4969dd59489886337c10529221ff81df0757d73997aff0d23f627
MD5 536eb6d8be4eb4d67faa6ca41d53dbbf
BLAKE2b-256 40f40fc422c30cab255b111665d25bcd1047206f243d77675f3b1f3da20c7c8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-py3-none-any.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: plum-2.8.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 146.9 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1a2bda11c59bc2b27503966687e031166040b1135fcb3c118918c7850c7a2e23
MD5 320f3467ac1c060c9060b0709de11683
BLAKE2b-256 9c6fb166d73340ebb7dc91e9a201ae60e9bb55e3988be464bb47e528aa846843

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp313-cp313-win_amd64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 25a827bd5f77a515620d37147c4f31124765f71f9ec53ba3c0c285f8f22e0fbd
MD5 c3398ddcbe83481081efda0be8347c4c
BLAKE2b-256 45255c7ab1517f522914861010108d5263aab0ff508bc621487c6dd579ea697d

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 2fcd859f95baa61bb60cb7000600386cad34577ba1751d64a558dfb37bd95664
MD5 baf919434d122cf8f96ea5003a9fb6fc
BLAKE2b-256 58dfe2cb205569a3edb39c1355cb8faf2007f369ae17c8c9cf1b369dc7dbf1fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: plum-2.8.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 146.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2e723d833a68ad56e37f88be79c5dd2b7d3ef4bba5ca4669557e6531e841dc69
MD5 0360207de0ece8b4eafcd2ce9f7d9fb4
BLAKE2b-256 7bb1c5116926ec5add2cd4f97a70b2b5091637aa2516228a3e75ec279da79b1f

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp312-cp312-win_amd64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f5b5d88b2f2f8d6db5c4ceb194d454dd64a6410d184ca6927de38c1c8b0caef0
MD5 75e3a926b2776cc6f637c6bdfba13e61
BLAKE2b-256 737152cc28f16be1fae201264f351f5d53db3aed96dcddce00e8c909228f3646

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 be888c1da70cd46b6e64aaa2d688d12e9d52cbc3510c9678c79a2949203ffac9
MD5 06fe74ae5b2f4a2b58d348c793b03386
BLAKE2b-256 6bdfc064b45f1c4e3c7c2f1f66671f588dd1e701fa85b6cca6b6e3f59f73b00c

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: plum-2.8.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 146.5 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 035252c28b2af9c0428bc45f2c9fcd48676002be874be8840c0674c4e45d1a7a
MD5 8bd7c1028f61b5e3a2f3d8d353419533
BLAKE2b-256 754c513c2a2dff6589972ce52704da164314bf4a6936c1d04074eb5eab29e190

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp311-cp311-win_amd64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 85071d2cd63e5be9c9789f418b813dfbf5abd6f2916eda8484f5194da31af2ff
MD5 d5238d6cfde0ca558c0c53cd5d572f17
BLAKE2b-256 493931f8cf1c4ae0a6109d51cd462e3eb8937b2908ee291d87ac6fdd6ca9b784

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f5c079c9d08de764872d579f5bce942711b1282d501f2f7d3f88ee33c52d02a3
MD5 1acddb019457a426d48f32b6478244e4
BLAKE2b-256 448bcadac120e6db45791a2d709895619edd348136ffd6976065a10cca1de09e

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: plum-2.8.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 146.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for plum-2.8.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 818810e33baf890c8f50bec05271a88c657748b3151cdf0d40ad755ca473f2d2
MD5 09ebd07bb0c950f6bf91fd5a3e008fd9
BLAKE2b-256 a1368b57ac84b94539308e022668cbb0cbccc53d64983344cf942a3b27bb76ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp310-cp310-win_amd64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ce54da5438c5baa14064d7288f1d75a1eaa73dc47c69c37b2b0e14d16666882c
MD5 6e57e7bed7f8934dedd488c79ab81a97
BLAKE2b-256 e558ac4a9ec54ce03d31541cde9d5f8578cab21681c34213631bcf230b899a0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file plum-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8ddae3d75e5bd2517dff0b7c7a9bcd24d5b7e802d567ca7a8d438a8098a378d4
MD5 f3cd4f63524be7b734f83e08b62eaa8a
BLAKE2b-256 b8a8f61af855f24391262111f20658e929c88e8a835cfdc4c8de37a731e1608e

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl:

Publisher: publish_new_pypi_name.yml on beartype/plum

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page