Multiple Dispatch
|Build Status| |Coverage Status| |Version Status|
A relatively sane approach to multiple dispatch in Python.
This implementation of multiple dispatch is efficient, mostly complete,
performs static analysis to avoid conflicts, and provides optional namespace
support. It looks good too.
See the documentation at https://multiple-dispatch.readthedocs.io/
Example
.. code-block:: python
from multipledispatch import dispatch
@dispatch(int, int)
... def add(x, y):
... return x + y
@dispatch(object, object)
... def add(x, y):
... return "%s + %s" % (x, y)
add(1, 2)
3
add(1, 'hello')
'1 + hello'
What this does
-
Dispatches on all non-keyword arguments
-
Supports inheritance
-
Supports instance methods
-
Supports union types, e.g. (int, float)
-
Supports builtin abstract classes, e.g. Iterator, Number, ...
-
Caches for fast repeated lookup
-
Identifies possible ambiguities at function definition time
-
Provides hints to resolve ambiguities when they occur
-
Supports namespaces with optional keyword arguments
-
Supports variadic dispatch
What this doesn't do
.. code-block:: python
a = arbitrary_type()
@dispatch(a, a)
def are_same_type(x, y):
return True
- Efficient update: The addition of a new signature requires a full resolve of
the whole function. This becomes troublesome after you get to a few hundred
type signatures.
Installation and Dependencies
multipledispatch
is on the Python Package Index (PyPI):
::
pip install multipledispatch
It is Pure-Python and depends only on the standard library.
It is a light weight dependency.
License
New BSD. See License file
_.
Links
Five-minute Multimethods in Python by Guido
_multimethods package on PyPI
_singledispatch in Python 3.4's functools
_Clojure Protocols
_Julia methods docs
_Karpinksi notebook: *The Design Impact of Multiple Dispatch*
_Wikipedia article
_PEP 3124 - *Overloading, Generic Functions, Interfaces, and Adaptation*
_
.. _Five-minute Multimethods in Python by Guido
:
http://www.artima.com/weblogs/viewpost.jsp?thread=101605
.. _multimethods package on PyPI
:
https://pypi.python.org/pypi/multimethods
.. _singledispatch in Python 3.4's functools
:
http://docs.python.org/3.4/library/functools.html#functools.singledispatch
.. _Clojure Protocols
:
http://clojure.org/protocols
.. _Julia methods docs
:
https://docs.julialang.org/en/v1/manual/methods/
.. _Karpinksi notebook: *The Design Impact of Multiple Dispatch*
:
http://nbviewer.ipython.org/gist/StefanKarpinski/b8fe9dbb36c1427b9f22
.. _Wikipedia article
:
http://en.wikipedia.org/wiki/Multiple_dispatch
.. _PEP 3124 - *Overloading, Generic Functions, Interfaces, and Adaptation*
:
http://legacy.python.org/dev/peps/pep-3124/
.. |Build Status| image:: https://travis-ci.org/mrocklin/multipledispatch.svg
:target: https://travis-ci.org/mrocklin/multipledispatch
.. |Version Status| image:: https://pypip.in/v/multipledispatch/badge.svg
:target: https://img.shields.io/pypi/v/multipledispatch.svg
.. |Coverage Status| image:: https://coveralls.io/repos/mrocklin/multipledispatch/badge.svg
:target: https://coveralls.io/r/mrocklin/multipledispatch
.. _License file: https://github.com/mrocklin/multipledispatch/blob/master/LICENSE.txt