bagof-dispatchers
bagof.dispatchers provides multiple dispatch for Python. Register several
implementations of a function, and each call runs the implementation whose
declared parameter types most specifically match the runtime types of the
arguments, the way Julia's multiple dispatch works.
>>> from bagof.dispatchers import dispatch
>>> @dispatch
... def area(width: int, height: int) -> int:
... return width * height
>>> @dispatch
... def area(radius: float) -> float:
... return 3 * radius * radius
>>> area(3, 4)
12
>>> area(2.0)
12.0
The second def named area does not replace the first. It adds an
overload, and the name area stays bound to a single dispatched function
that chooses between the two on every call. Getting started
walks through registering overloads in more detail.
What sets it apart
- Dispatch reads the full vocabulary of type hints, not only plain classes.
Unions, literals, generics,
TypedDict, and type variables ordered by their declared variance all narrow which overload applies. See Variance. Exact[C]matches a value only when its type is exactlyC, for the cases where a subclass should not quietly take over another overload's place;boolis otherwise also anint. See Exact types.- A call that matches no overload, or that matches two equally specific
overloads, is always a clear error rather than a silent guess.
NoMethodErroris aTypeErrorcarrying the closest candidates, andAmbiguousMethodErrornames the overloads that tie. See When two overloads are equally specific and When nothing matches. - Dispatch is aware of argument names as well as positions, so a call binds by position or by keyword, either way, against the same overloads. See Registering overloads.
- Registries are explicit.
@dispatchkeeps functions separate per module; build aDispatcherwhen several modules should extend one shared function instead. - The library supports Python 3.8 through the current release, and depends
on nothing beyond
typing_extensions.
Install
pip install git+https://github.com/bagofseeds/bagof-dispatchers.git
Status
The project is at an early stage: the API is still settling, and some of it may still move. Issues and ideas are welcome at bagofseeds/bagof-dispatchers.
bagof.dispatchers is the low-level root of the bagof family. It owns the
hint subtype relation and the introspection helpers the other bags build on;
see Part of bagof and the project overview. For how
it compares with plum, multipledispatch, functools.singledispatch, and
Julia's own multiple dispatch, see the comparison page.
Metadata
Release files for bagof-dispatchers 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bagof_dispatchers-0.1.0.tar.gz | 239.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bagof_dispatchers-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 367.8 kB
Release files / bagof_dispatchers-0.1.0.tar.gz
| Download URL | bagof_dispatchers-0.1.0.tar.gz |
|---|---|
| Size | 239.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
088b0f0963a4f11a6a27cf449be4b2c2536aed36d9d114b77044324c8725d9bf
|
|
BLAKE2b-256 checksum How to use checksums |
c4ef02cfb2c7c5fbcdc1ecc871f97165d274aa45556fb23a515d3ece08a9a6b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / bagof_dispatchers-0.1.0-py3-none-any.whl
| Download URL | bagof_dispatchers-0.1.0-py3-none-any.whl |
|---|---|
| Size | 128.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
be3d465debbd8082c6bf7847edf3adf5886287b736e9e80d36aa5087ad845450
|
|
BLAKE2b-256 checksum How to use checksums |
6b4b127b63d7f6815d1c756dfde38d462977da415090e3a6c1749ebd7f9aa4ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|