Skip to main content

Multiple dispatch

Project description

Multiple Dispatch 2
=================

|Build Status| |Coverage Status| |Version Status| |Downloads|

This is a fork from `mroklin's multipledispatch<https://github.com/mrocklin/multipledispatch>`_, which is designed to fully support python 3's type annotation.

And it only supports python 3.3+.

Example
-------

.. code-block:: python

>>> from multipledispatch2 import dispatch

>>> @dispatch
... def add(x: int, y: int) -> int:
... return x + y

>>> @dispatch
... 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

What this doesn't do
--------------------

- Vararg dispatch

.. code-block:: python

import typing

@dispatch
def add(*args: typing.Iterable[int]):
...

- Diagonal dispatch

.. code-block:: python

a = arbitrary_type()
@dispatch
def are_same_type(x: a, y: a) -> bool:
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. It can be mitigated by halting and restarting ordering see
(``halt_ordering`` and ``restart_ordering`` functions for more information.)


Installation and Dependencies
-----------------------------

``multipledispatch2`` is on the Python Package Index (PyPI):

::

pip install multipledispatch2

or

::

easy_install multipledispatch2


``multipledispatch2`` is only supports Python 3.3+. It is pure Python and requires no dependencies beyond the standard
library.

It is, in short, a light weight dependency.


License
-------

New BSD. See License_.


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*`_
- `PEP 0484 - *Type Hints*`_

.. _`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`:
http://julia.readthedocs.org/en/latest/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/
.. _`PEP 0484 - *Type Hints*`:
https://www.python.org/dev/peps/pep-0484/

.. |Build Status| image:: https://travis-ci.org/mrocklin/multipledispatch.png
:target: https://travis-ci.org/mrocklin/multipledispatch
.. |Version Status| image:: https://pypip.in/v/multipledispatch/badge.png
:target: https://pypi.python.org/pypi/multipledispatch/
.. |Downloads| image:: https://pypip.in/d/multipledispatch/badge.png
:target: https://pypi.python.org/pypi/multipledispatch/
.. |Coverage Status| image:: https://coveralls.io/repos/mrocklin/multipledispatch/badge.png
:target: https://coveralls.io/r/mrocklin/multipledispatch
.. _License: https://github.com/mrocklin/multipledispatch/blob/master/LICENSE.txt

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

multipledispatch2-0.3.0.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file multipledispatch2-0.3.0.tar.gz.

File metadata

File hashes

Hashes for multipledispatch2-0.3.0.tar.gz
Algorithm Hash digest
SHA256 173ff6b31fe05a70becc57693dfa3d2be2c18889702e96beb650ea5784ed8887
MD5 db6ca0cd18b1728f2783e81ae932630d
BLAKE2b-256 64d3256ebcb26b89b29cd60fef771f0d2dc29a92c80542f203c8a267d0ee6b44

See more details on using hashes here.

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