Multiple dispatch in Python (with additional features for the CoLA library)
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 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
Built Distribution
File details
Details for the file cola_plum_dispatch-0.1.2.tar.gz
.
File metadata
- Download URL: cola_plum_dispatch-0.1.2.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a89eb7a86f484c9c332014e8d6617e2f3e2613c3332dfde368a722bc31306b3f |
|
MD5 | 36b5da7babce55b7add1d681b6e003bb |
|
BLAKE2b-256 | 646bc46fdd05b4379955596cdb949e6e22621fa5424b940e45a86b678332589e |
Provenance
File details
Details for the file cola_plum_dispatch-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: cola_plum_dispatch-0.1.2-py3-none-any.whl
- Upload date:
- Size: 31.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88bbaef519fe56625665b560dea6cda40cd6db203a80be07f30774b179c0dc46 |
|
MD5 | 9229018d1e979974ed985caf21aec1ca |
|
BLAKE2b-256 | 7b2387ee58219e15492f9d745e4884024c704cd487e3938efb0878074f5e620b |