multipledispatch2

Multiple dispatch


Keywords
dispatch
License
BSD-3-Clause
Install
pip install multipledispatch2==0.4.0

Documentation

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

>>> 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
import typing

@dispatch
def add(*args: typing.Iterable[int]):
    ...
  • Diagonal dispatch
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