Hook
This module contains the Hook class, which is used for event handling, and for defining additional behaviors to the class instances which own the Hook.
Hook
¶
Bases: MutableSequence
A Hook stores a list of callable objects to be called for handling certain events. A Hook itself is callable, which invokes the callables stored in its list. If the callables stored by the Hook return list-like objects or dict-like objects, their returned results are accumulated, and then those accumulated results are finally returned by the Hook.
Source code in evotorch/tools/hook.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
args
property
¶
Positional arguments that will be passed to the stored callables
kwargs
property
¶
Keyword arguments that will be passed to the stored callables
__call__(*args, **kwargs)
¶
Call every callable object stored by the Hook. The results of the stored callable objects (which can be dict-like or list-like objects) are accumulated and finally returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args
|
Any
|
Additional positional arguments to be passed to the stored callables. |
()
|
kwargs
|
Any
|
Additional keyword arguments to be passed to the stored keyword arguments. |
{}
|
Source code in evotorch/tools/hook.py
__init__(callables=None, *, args=None, kwargs=None)
¶
Initialize the Hook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callables
|
Optional[Iterable[Callable]]
|
A sequence of callables to be stored by the Hook. |
None
|
args
|
Optional[Iterable]
|
Positional arguments which, when the Hook is called,
are to be passed to every callable stored by the Hook.
Please note that these positional arguments will be passed
as the leftmost arguments, and, the other positional
arguments passed via the |
None
|
kwargs
|
Optional[Mapping]
|
Keyword arguments which, when the Hook is called,
are to be passed to every callable stored by the Hook.
Please note that these keyword arguments could be overriden
by the keyword arguments passed via the |
None
|