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_dispatch-2.8.0.tar.gz (241.3 kB view details)

Uploaded Source

Built Distributions

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

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

Uploaded Python 3

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

Uploaded CPython 3.13Windows x86-64

plum_dispatch-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (193.6 kB view details)

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

plum_dispatch-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl (165.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

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

Uploaded CPython 3.12Windows x86-64

plum_dispatch-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (194.4 kB view details)

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

plum_dispatch-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl (165.3 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

plum_dispatch-2.8.0-cp311-cp311-win_amd64.whl (146.6 kB view details)

Uploaded CPython 3.11Windows x86-64

plum_dispatch-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (192.1 kB view details)

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

plum_dispatch-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl (162.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

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

Uploaded CPython 3.10Windows x86-64

plum_dispatch-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (194.9 kB view details)

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

plum_dispatch-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl (164.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for plum_dispatch-2.8.0.tar.gz
Algorithm Hash digest
SHA256 453fc7bc67d2a39492c834b00c94d816871d148b5a0a5d3f49e2bbf2391e2619
MD5 ab91af916ccb1efbb651d15824d68821
BLAKE2b-256 5ecf2f2b0dd84edbb2951a845ba98d29f651bbbd467039c368fcfa22904905cd

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-py3-none-any.whl.

File metadata

  • Download URL: plum_dispatch-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_dispatch-2.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5dfffc46de1d8208ba281fdb0f41d6065c7341c1fde76fc83b4b78810c30c7e
MD5 53cd3909878014498e3a9ab3e762ac04
BLAKE2b-256 4b7d6d1a0a5851fb590564045471bdd6f405426ae4214cafe530ceba297e5c0d

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 53bc084b335e5071b72353830a9f2cdacd58c9e959089be5778b6d638b5062f9
MD5 58954408dbf2e97162fc860471f257f9
BLAKE2b-256 fbd2772d1e3071971a30e70b78df421f51788354337a7f94957d910ba3cd8a5a

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-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_dispatch-2.8.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f88aaf08a562e7a559851679a051b32306b67fd403ed6070915a966f048a83e9
MD5 fa9dbc52d4cf7d61d34246e483736c24
BLAKE2b-256 592f94e4e62ad437996341d03471bd398f3f6bf4c5f3a1fa524ba53f02b6896d

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 d60c0040207495afddab3f80c498a5e8f9dab29f8d143468c51d668fe70ed291
MD5 ac5af84bda0537b5ce8ccce26054e36d
BLAKE2b-256 6825380300b3468417999ca0db0522eeae1017798519fe28753e8ad110718174

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c0e6146881b71eb6a6355d18476bed356071d1dff45a7cf4ae31f7f4bffb01b9
MD5 18c9f1b38d9239fcda04499cdd3dfc9a
BLAKE2b-256 6b01a8947d5c92824a6bc019696e5d9f150178736ecd8e6766db38b38e0f0fda

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-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_dispatch-2.8.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 599025e7bd7e16f198e5ccaad0ca51036fcbeb43eb41525a5ef705b9a25bb192
MD5 6d814e779daf826cc1e6cb76159261b6
BLAKE2b-256 25feb1dbbdd1864d4ca825b3b5079fca852ea5e5a4f223aa866702f751b02dba

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fca6b6dbed8c686f6f827e9c95f60334ba70ee27cccef1f44a50d42a30221748
MD5 d6f133e154c10aa6dd460a2b75ab428e
BLAKE2b-256 e9a3cdb8130cc05a13c75237d2be4821f38c0b99d1a64980b6529d788a43a501

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9c7d033348a6d60940476b5be9132dd1a87cb0d0ba335ad3bd4213137162bbf5
MD5 c3f861b08836d48ece14c2e3708ef5d2
BLAKE2b-256 5c410115bb8a2a6d06f9698fec20ede13350897271b46173b3d9e75f1711f73f

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-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_dispatch-2.8.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 71a8619fe9017d9741ab96c73443b50afe1b7159202d94b332a64fa2ed760cdf
MD5 c246fe3ed20c9cd160bc992a4805b1d8
BLAKE2b-256 6551ddb87d0f7190d176d2c22174b6ebc02c5d6d1d571dec2a5ab3f388e8a1c8

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2b3aa8ff3f5a356e4a8f659bb35ce52c2dd9c67bfefb89fe35204bc9be3be77a
MD5 9dc9ac9f45bf32d774b79352a996ec7d
BLAKE2b-256 f2acc502295727dd9c9bd2105c130ae4420762536faf411ed94b5b13c31b3e6b

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8379a078dd64d5730549aceb64aeddedb8ce284ea24b6acaf519aea7604fc9d7
MD5 abd18083ec9ebf56b2f333381a5454ef
BLAKE2b-256 a78e63d8829e317afabcd8380a8b7aa31e6281c375e912afe178f03c198514f3

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-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_dispatch-2.8.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c60cc5d3d1363f8e199473bc363362c6a55d604e4df993a8248203ee0491db6c
MD5 a783438f14b51d5f43066b63a9683718
BLAKE2b-256 263e9ec5ea048c9ce9cc833223e1e5c07582089df231afc2b9f120731bf2fc16

See more details on using hashes here.

Provenance

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

Publisher: publish.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_dispatch-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.8.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8ae9dc811e4970bcf8ae6322349a9da2b46c17b61195283401ddfdd81c6771e0
MD5 2f23a8a1e3ade61f8e6ad21914aabb50
BLAKE2b-256 46fc7ce3113c89dd24cf8243f96891586454b781a4e3dbf3070dfb958284160b

See more details on using hashes here.

Provenance

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

Publisher: publish.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