# Buildsheet autogenerated by ravenadm tool -- Do not edit. NAMEBASE= python-singledispatch VERSION= 3.4.0.3 KEYWORDS= python devel VARIANTS= py27 py36 py37 SDESC[py36]= Backport of single-dispatch functions (PY 36) SDESC[py37]= Backport of single-dispatch functions (PY 37) SDESC[py27]= Backport of single-dispatch functions (PY 27) HOMEPAGE= http://docs.python.org/3/library/functools.html#functools.singledispatch CONTACT= Python_Automaton[python@ironwolf.systems] DOWNLOAD_GROUPS= main SITES[main]= PYPI/s/singledispatch DISTFILE[1]= singledispatch-3.4.0.3.tar.gz:main DF_INDEX= 1 SPKGS[py36]= single SPKGS[py37]= single SPKGS[py27]= single OPTIONS_AVAILABLE= PY27 PY36 PY37 OPTIONS_STANDARD= none VOPTS[py36]= PY27=OFF PY36=ON PY37=OFF VOPTS[py37]= PY27=OFF PY36=OFF PY37=ON VOPTS[py27]= PY27=ON PY36=OFF PY37=OFF DISTNAME= singledispatch-3.4.0.3 GENERATED= yes [PY36].BUILDRUN_DEPENDS_ON= python-six:single:py36 [PY36].USES_ON= python:py36 [PY37].BUILDRUN_DEPENDS_ON= python-six:single:py37 [PY37].USES_ON= python:py37 [PY27].BUILDRUN_DEPENDS_ON= python-six:single:py27 [PY27].USES_ON= python:py27 [FILE:3046:descriptions/desc.single] ============== singledispatch ============== `PEP 443 `_ proposed to expose a mechanism in the ``functools`` standard library module in Python 3.4 that provides a simple form of generic programming known as single-dispatch generic functions. This library is a backport of this functionality to Python 2.6 - 3.3. To define a generic function, decorate it with the ``@singledispatch`` decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly:: >>> from singledispatch import singledispatch >>> @singledispatch ... def fun(arg, verbose=False): ... if verbose: ... print("Let me just say,", end=" ") ... print(arg) To add overloaded implementations to the function, use the ``register()`` attribute of the generic function. It is a decorator, taking a type parameter and decorating a function implementing the operation for that type:: >>> @fun.register(int) ... def _(arg, verbose=False): ... if verbose: ... print("Strength in numbers, eh?", end=" ") ... print(arg) ... >>> @fun.register(list) ... def _(arg, verbose=False): ... if verbose: ... print("Enumerate this:") ... for i, elem in enumerate(arg): ... print(i, elem) To enable registering lambdas and pre-existing functions, the ``register()`` attribute can be used in a functional form:: >>> def nothing(arg, verbose=False): ... print("Nothing.") ... >>> fun.register(type(None), nothing) The ``register()`` attribute returns the undecorated function which enables decorator stacking, pickling, as well as creating unit tests for each variant independently:: >>> @fun.register(float) ... @fun.register(Decimal) ... def fun_num(arg, verbose=False): ... if verbose: ... print("Half of your number:", end=" ") ... print(arg / 2) ... >>> fun_num is fun False When called, the generic function dispatches on the type of the first argument:: >>> fun("Hello, world.") Hello, world. >>> fun("test.", verbose=True) Let me just say, test. >>> fun(42, verbose=True) Strength in numbers, eh? 42 >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True) Enumerate this: 0 spam 1 spam 2 eggs 3 spam >>> fun(None) Nothing. >>> fun(1.23) 0.615 Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found. To check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute:: >>> fun.dispatch(float) >>> fun.dispatch(dict) # note: default implementation To access all registered implementations, use the read-only ``registry`` attribute:: >>> fun.registry.keys() [FILE:108:distinfo] 5b06af87df13818d14f08a028e42f566640aef80805c3b50c5056b086e3c2b9c 9529 singledispatch-3.4.0.3.tar.gz