1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 from .overloader import Overloader
22
24 """
25 Overloads an operation.
26
27 The decorated operation is replaced entirely, its docstring is copied to
28 the replacement operation.
29
30 Parameters:
31
32 `overloads` :: (Overload...)
33 a collection of rules indicating which operation to call for which
34 combination of arguments. At least one overload must be given.
35
36 `is_method` :: bool
37 If True, the decorated is considered to be a method and a self
38 parameter is added to each overload automatically (don't add self
39 parameters manually). If False, the decorated is treated as a
40 function.
41
42 Usage example::
43
44 def __init_xy(self, x, y):
45 self.__storage = Storage(x, y)
46
47 def __init_storage(self, storage):
48 self.__storage = storage
49
50 @overloaded((
51 Overload(__init_xy,
52 Param("x", default=0),
53 Param("y", default=0)),
54 Overload(__init_storage,
55 Param("storage"))))
56 def __init__(self):
57 "docstring"
58 pass
59 """
60 overloader = Overloader(overloads, is_method)
61
62
63 def __overloaded(*args, **kwargs):
64 return overloader.process_call(args, kwargs)
65
66 def _overloaded(f):
67 __overloaded.__doc__ = f.__doc__
68 return __overloaded
69
70 return _overloaded
71