Multiple dispatch in Python
Project description
Plum: Multiple Dispatch in Python
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 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fenbux_plum_dispatch-0.0.2.tar.gz
.
File metadata
- Download URL: fenbux_plum_dispatch-0.0.2.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f353dac1f6457c1e934f2a6e5ba2ca5497189a39a80f32cf2c6e142f89556d1 |
|
MD5 | aa9bf7d9d85e7ad3d903fb323228cdf0 |
|
BLAKE2b-256 | 6894fdb65c477c2b49e24eb927e1bed8a256ba7c0521f23f667ca8db57051a75 |
File details
Details for the file fenbux_plum_dispatch-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: fenbux_plum_dispatch-0.0.2-py3-none-any.whl
- Upload date:
- Size: 32.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65558aa9004fecf4ebaf81046fda74fc6926a3c5f2061e7a93eed296e625d135 |
|
MD5 | f25d47e1b35f0e1440ae59d61daa8a75 |
|
BLAKE2b-256 | 7781ec5729fc6ee5c3ecd076bb5ed214fb9529739beed4b3462afef67f2a01a1 |