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.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-2.9.0-py3-none-any.whl (45.3 kB view details)

Uploaded Python 3

plum-2.9.0-cp313-cp313-win_amd64.whl (152.5 kB view details)

Uploaded CPython 3.13Windows x86-64

plum-2.9.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (204.6 kB view details)

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

plum-2.9.0-cp313-cp313-macosx_10_13_x86_64.whl (174.4 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

plum-2.9.0-cp312-cp312-win_amd64.whl (152.5 kB view details)

Uploaded CPython 3.12Windows x86-64

plum-2.9.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (205.9 kB view details)

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

plum-2.9.0-cp312-cp312-macosx_10_13_x86_64.whl (175.1 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

plum-2.9.0-cp311-cp311-win_amd64.whl (152.0 kB view details)

Uploaded CPython 3.11Windows x86-64

plum-2.9.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (203.3 kB view details)

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

plum-2.9.0-cp311-cp311-macosx_10_9_x86_64.whl (171.9 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

plum-2.9.0-cp310-cp310-win_amd64.whl (152.5 kB view details)

Uploaded CPython 3.10Windows x86-64

plum-2.9.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (206.0 kB view details)

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

plum-2.9.0-cp310-cp310-macosx_10_9_x86_64.whl (174.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: plum-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-2.9.0.tar.gz
Algorithm Hash digest
SHA256 c3f7c0294d8194bc54e5948d86ecd74ae9869f0f6326472cb24063a1befe400d
MD5 8b6451a146b269d88265c61702e6cee5
BLAKE2b-256 7a185f07cdf83147a739dcadfb6c1e17c26f4ffe0ca41a3c96dbfa20a7a74812

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: plum-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-2.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 92838bb6df686471c4c63692a3a7f05eed147b127fd10f57e49133d223d0e419
MD5 feecfb2a4fb10bb7d22743456eb6457d
BLAKE2b-256 f7d590283068ef65329bc103924aa76c566f5716635303be3a60cb237e12926c

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for plum-2.9.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 aa6d12fd43278b7700d7c37f80d75c3878641f7571ecd8008bba236d1aaf9ab3
MD5 2d93263189e3664a18da90c1dd3c3c2e
BLAKE2b-256 cf02baa3115e07b14f7f598ce84ab22bc75cceec9419d047f2b8a70bf6ee21a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.9.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.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-2.9.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bae72041c67e303078387f6376be6e5bc20eca477c242e59ced7cc2b29939f09
MD5 1d136230bf1e50e82b0353cf22867dde
BLAKE2b-256 c45e211281f42345847841bd14f8f6508a799ae11c59a0e4ad1b6f3469e124ec

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for plum-2.9.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 1d38955ab44a8ffe9e7b73801a4cac9db593d7a8a3e56bbb818ff2b5a3667f06
MD5 9d3bfe69d23bdc12f3d2ba2a8b94a1cb
BLAKE2b-256 1fb14a0147f290b3a067249456b63e76f336f42aa378b64e20809628cb103668

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for plum-2.9.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 19bf8313af75d19ca4c37cfd119b5e55899bba0282dfb91a90e70828d3098dc6
MD5 90741fbd90e5c4d7d168334ffe650808
BLAKE2b-256 446d3e1051c7c88a53a91ce7788eea5f08b952e39aab958068018740030e13c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.9.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.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-2.9.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0abac91e0732614e6495191721b6b50c118e2294099087f3552f783767154c12
MD5 2b7a315e21bfc5f568d79c217e414cad
BLAKE2b-256 f9383d7b97f1cd38db377718a078a6273fa2c07f6b83f2fcd3285be91980a3b6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for plum-2.9.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 dfeb3db3a91468d10dd19fb17d7c0a9e77b489fea567294a875dfc0ff73b63e3
MD5 7ea22d41c9924cb4ab79d5835360926e
BLAKE2b-256 3f39af6727d3c64ab627b7f7a5ccb593ae8eb46d9c6fa1d1f44fb1a5653aa69d

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for plum-2.9.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 311aa83a37e5f5351a65b6b45e7fe25d780a6cff13a423d58e6ec92959e48b92
MD5 7d8afc64b05db75d00917e834052e0f6
BLAKE2b-256 d47fb06bcc67f8b4fb48d4e7be1ae02f6ce87a71759b04d9877f1a42f4a64259

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.9.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.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-2.9.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 90bb04507d39d5b1a13e11960dd5365ab708111124e77318e901c0735129af7e
MD5 5bb0445b2a1bad95b9ab9e0042c79566
BLAKE2b-256 5d98df4ef8a6e4f1eb286160b93603726e4b6fadfed03f03c34342e6f864a1bd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for plum-2.9.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 153ba9f4322e8e632bbaf72f2462df317e261ea598b6a30c1d045d7f4fde89b7
MD5 ed761f4908870f7f851c3cfe41979a1b
BLAKE2b-256 7ade75fba838b6ab8b13897f0357fb4a1882579c7f6f49cd86c55c500651d475

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for plum-2.9.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 86f60a77b05da656626a48843303799e0cbea7a198ca3fca75c49f08644e313e
MD5 428ed21cc951671d96c88132f8df26a3
BLAKE2b-256 b4fcb3f4bfdd24acd306ba59e70baade7afbe62227e7761b073ced9deca1e7f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for plum-2.9.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.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-2.9.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 10637b6d0a58463e0a3a2ea48e975138e07b2e57c49572d08e4308e7e66da983
MD5 045733e39dbe180a633a99578f613513
BLAKE2b-256 0c1845617658883c7231ab010af6d383ce75c4cf5b429e6361199d9e3fa6c102

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for plum-2.9.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 03e2c1802f41c29a2922cf773039b4ebdb97df9f3e9e13896453869b4f8c946d
MD5 fd018b39b91ddd6944933de2fd86dfa9
BLAKE2b-256 b956b63c4ea2e4ebe97e4a52e2b83435d3be9dcee31734e0dc4af67a01b85fc9

See more details on using hashes here.

Provenance

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