1 # Buildsheet autogenerated by ravenadm tool -- Do not edit.
3 NAMEBASE= python-singledispatch
6 VARIANTS= py27 py36 py37
7 SDESC[py36]= Backport of single-dispatch functions (PY 36)
8 SDESC[py37]= Backport of single-dispatch functions (PY 37)
9 SDESC[py27]= Backport of single-dispatch functions (PY 27)
10 HOMEPAGE= http://docs.python.org/3/library/functools.html#functools.singledispatch
11 CONTACT= Python_Automaton[python@ironwolf.systems]
14 SITES[main]= PYPI/s/singledispatch
15 DISTFILE[1]= singledispatch-3.4.0.3.tar.gz:main
21 OPTIONS_AVAILABLE= PY27 PY36 PY37
22 OPTIONS_STANDARD= none
23 VOPTS[py36]= PY27=OFF PY36=ON PY37=OFF
24 VOPTS[py37]= PY27=OFF PY36=OFF PY37=ON
25 VOPTS[py27]= PY27=ON PY36=OFF PY37=OFF
27 DISTNAME= singledispatch-3.4.0.3
31 [PY36].BUILDRUN_DEPENDS_ON= python-six:single:py36
32 [PY36].USES_ON= python:py36
34 [PY37].BUILDRUN_DEPENDS_ON= python-six:single:py37
35 [PY37].USES_ON= python:py37
37 [PY27].BUILDRUN_DEPENDS_ON= python-six:single:py27
38 [PY27].USES_ON= python:py27
40 [FILE:3046:descriptions/desc.single]
45 `PEP 443 <http://www.python.org/dev/peps/pep-0443/>`_ proposed to expose
46 a mechanism in the ``functools`` standard library module in Python 3.4
47 that provides a simple form of generic programming known as
48 single-dispatch generic functions.
50 This library is a backport of this functionality to Python 2.6 - 3.3.
52 To define a generic function, decorate it with the ``@singledispatch``
53 decorator. Note that the dispatch happens on the type of the first
54 argument, create your function accordingly::
56 >>> from singledispatch import singledispatch
58 ... def fun(arg, verbose=False):
60 ... print("Let me just say,", end=" ")
63 To add overloaded implementations to the function, use the
64 ``register()`` attribute of the generic function. It is a decorator,
65 taking a type parameter and decorating a function implementing the
66 operation for that type::
68 >>> @fun.register(int)
69 ... def _(arg, verbose=False):
71 ... print("Strength in numbers, eh?", end=" ")
74 >>> @fun.register(list)
75 ... def _(arg, verbose=False):
77 ... print("Enumerate this:")
78 ... for i, elem in enumerate(arg):
81 To enable registering lambdas and pre-existing functions, the
82 ``register()`` attribute can be used in a functional form::
84 >>> def nothing(arg, verbose=False):
87 >>> fun.register(type(None), nothing)
89 The ``register()`` attribute returns the undecorated function which
90 enables decorator stacking, pickling, as well as creating unit tests for
91 each variant independently::
93 >>> @fun.register(float)
94 ... @fun.register(Decimal)
95 ... def fun_num(arg, verbose=False):
97 ... print("Half of your number:", end=" ")
103 When called, the generic function dispatches on the type of the first
106 >>> fun("Hello, world.")
108 >>> fun("test.", verbose=True)
109 Let me just say, test.
110 >>> fun(42, verbose=True)
111 Strength in numbers, eh? 42
112 >>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
123 Where there is no registered implementation for a specific type, its
124 method resolution order is used to find a more generic implementation.
125 The original function decorated with ``@singledispatch`` is registered
126 for the base ``object`` type, which means it is used if no better
127 implementation is found.
129 To check which implementation will the generic function choose for
130 a given type, use the ``dispatch()`` attribute::
132 >>> fun.dispatch(float)
133 <function fun_num at 0x1035a2840>
134 >>> fun.dispatch(dict) # note: default implementation
135 <function fun at 0x103fe0000>
137 To access all registered implementations, use the read-only ``registry``
140 >>> fun.registry.keys()
144 5b06af87df13818d14f08a028e42f566640aef80805c3b50c5056b086e3c2b9c 9529 singledispatch-3.4.0.3.tar.gz