4.13. Help With Creating Function Keys¶
If you need to create a complicated key argument to (for example)
natsorted() that is actually multiple functions called one after the other,
the following function can help you easily perform this action. It is
used internally to
natsort, and has been exposed publically for
the convenience of the user.
Chain a list of single-argument functions together and return.
The functions are applied in list order, and the output of the previous functions is passed to the next function.
Parameters: functions (list) – A list of single-argument functions to chain together. Returns: Return type: A single argument function.
Chain several functions together!
>>> funcs = [lambda x: x * 4, len, lambda x: x + 5] >>> func = chain_functions(funcs) >>> func('hey') 17