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