Skip to main content

Multiple dispatch in Python (with additional features for the CoLA library)

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.8 or higher.

pip install cola-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...'

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

cola_plum_dispatch-0.1.3.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

cola_plum_dispatch-0.1.3-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file cola_plum_dispatch-0.1.3.tar.gz.

File metadata

  • Download URL: cola_plum_dispatch-0.1.3.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for cola_plum_dispatch-0.1.3.tar.gz
Algorithm Hash digest
SHA256 367e17ca9784b8c72ca2082a74a9686feecfe4bf28341aee751fa87b4841f1b4
MD5 f09f2e66a532ca4f6458e55f981ab529
BLAKE2b-256 9ff43266cd8fa133ab8311bc125dc8697e3f3760e08968c4a2abed56431928b9

See more details on using hashes here.

Provenance

File details

Details for the file cola_plum_dispatch-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for cola_plum_dispatch-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2b53d35ea79965fac361ef330517fe1783d46ba92d89ebae4ac9819cc6493a5
MD5 6fe8fc605ae8a4947e32243adcb40682
BLAKE2b-256 96c4440362c607dfd8751d95afd436170fa98d76683fd1d89eaa6c9d22212f88

See more details on using hashes here.

Provenance

Supported by

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