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.9.0.tar.gz (244.5 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.9.0-py3-none-any.whl (45.3 kB view details)

Uploaded Python 3

plum_dispatch-2.9.0-cp313-cp313-win_amd64.whl (152.6 kB view details)

Uploaded CPython 3.13Windows x86-64

plum_dispatch-2.9.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (204.7 kB view details)

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

plum_dispatch-2.9.0-cp313-cp313-macosx_10_13_x86_64.whl (174.5 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

plum_dispatch-2.9.0-cp312-cp312-win_amd64.whl (152.6 kB view details)

Uploaded CPython 3.12Windows x86-64

plum_dispatch-2.9.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (206.0 kB view details)

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

plum_dispatch-2.9.0-cp312-cp312-macosx_10_13_x86_64.whl (175.2 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

plum_dispatch-2.9.0-cp311-cp311-win_amd64.whl (152.1 kB view details)

Uploaded CPython 3.11Windows x86-64

plum_dispatch-2.9.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (203.4 kB view details)

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

plum_dispatch-2.9.0-cp311-cp311-macosx_10_9_x86_64.whl (172.0 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

plum_dispatch-2.9.0-cp310-cp310-win_amd64.whl (152.6 kB view details)

Uploaded CPython 3.10Windows x86-64

plum_dispatch-2.9.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (206.1 kB view details)

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

plum_dispatch-2.9.0-cp310-cp310-macosx_10_9_x86_64.whl (174.1 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for plum_dispatch-2.9.0.tar.gz
Algorithm Hash digest
SHA256 fb45c5b2dd4dadd57def51bcf321dfa3a258df5c725f43adea7e7f6db3b79b52
MD5 5945817a5f190267d21a5a5b2da03805
BLAKE2b-256 1bb784146ae5ff6c40d11357acdb36aafe3db7e104de01c1026d8e1b0ce3e7f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-py3-none-any.whl.

File metadata

  • Download URL: plum_dispatch-2.9.0-py3-none-any.whl
  • Upload date:
  • Size: 45.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for plum_dispatch-2.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a516cdac5460343937a2b813562ac00b0eeac973ac023916e538610bdf34397
MD5 463ddf47e62ec23e7c62f3e479c36d65
BLAKE2b-256 64a7ee4d01d26032b060d379f44a29124180f756d907e3840d3bc450f8a0d2a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 a0d16daf09482a6588025e9f133a338d99474e8dd3c3dbbc4c5282a4fffe5a0e
MD5 4b7f64c906e81853a89762abe13e252f
BLAKE2b-256 ba9823d19326af0da251bb98a39f517ab6da983e5aad5eb9cc398b60933df2cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.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.9.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 36662d76ae6d87aaa041fade036d2242f0b88b68c9fb4c96b7ba0026320065d3
MD5 a1ff80fe653e34aed995345f6e1b3f86
BLAKE2b-256 fc297b3de57cd86430fd00d71bc5a8a078bf2c63d4ada7fdfb30853fe976a19f

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 78987caf558fbdd675a7a4a5c144069f1f9524610195123fb7d0f0234d0af01a
MD5 a0bfb6c78c39924dd5fcea3d81766947
BLAKE2b-256 cb1819e01dbbaececb75815abb37bc71e10d87a7561829b4eae47ec3b342c94f

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 beb9b92c6994404a0d2dc83c857263f5da49519575e5cbb8a13060652746c44c
MD5 723d561f3b37d2e18134dcbb68507fa5
BLAKE2b-256 e1a528a6c4e45c4465a6e1a30d65206e913c217690752dc3c9261d78a9187aed

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.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.9.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d39b20a1af776a39e4a0833e97c6258d938e5d874c7f3537bb7d772c39718c1e
MD5 63a14ac5b8074ab6841754e78e0ec263
BLAKE2b-256 4c4d5418649397b477ae08839564c97596ede5ef36523147899bb913bbe959d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 39a325b0b041fad687271d71abc11b5271d5a3b62af2c6b1271f4ececc3b59de
MD5 0cef5743ac924cb5a2fefc4151e30a93
BLAKE2b-256 82d6fc6591336731b5e291319ec3c43d81cd6cc7940c9079183b333b85ed4d77

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1050150b6ae3600ac19a406b23cb7d0707825b227f608a54b8be9994d1f4b710
MD5 7e347a19cc5ebb364e58003016ca0025
BLAKE2b-256 d80578c229ccce36fbd6dbc1cde698baae57313515ff1bff6ebad28be46c9386

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.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.9.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f4d34cab84908abbdff501e356296eba960ff5d5fc04668b71ebc6f9ff84fa2f
MD5 637ac6828ad04ef3bed65988704935da
BLAKE2b-256 6301eee83f936500e3e0100ac421483fa78768fec70214e4ce0241187ec3a2f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 25f62c2209b7bff00c6cdc25fe153ac09df304d7d833fab2852fc46fffbb7e87
MD5 bc28978a33c28e5f5ae3cad8ecbfa5a3
BLAKE2b-256 e0207aa36a01b1689d427a9fc20c80b1c0bc8340d1413c8071f08fb1e9a01939

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7e767a7e52f7d4cb3f10010024047d043c29d7ed0ab7df63bceef5371c44a2fa
MD5 f84f60a973cca169590c2a2acf057024
BLAKE2b-256 e23a578642ace42b78507a98e8b27ad7d8554d197b7ec67d28eab6806b0a0fc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.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.9.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7cec06a2f2545a2b09e8dde81be6f1512e1df850b3ab2fbf582ef79a10956b23
MD5 e6b02318feb1879178e3ecf6f5316d2a
BLAKE2b-256 3391a2879973fd40c985aeb55ffe522d7b91c10062f82b251b9ad202f063a3e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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.9.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for plum_dispatch-2.9.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6c5f5dfd1afd42dfd738c1b7701b9e03279c52b4952d6bb56e169564b1d48eb9
MD5 c28a14af25bdc968268b06df221e5e12
BLAKE2b-256 80e18fa7fffff5699fa9d7aae2c57de27f2b21a242189f8c102c80a0961dcc89

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum_dispatch-2.9.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