import os def exit_with_result(function, _exit=os._exit): result = function() if result: _exit(0) else: _exit(1)
The problem is that when you add arguments to a function, the parameterization leaks into your public API. This means that:
- You need to document the fact that these extra arguments (e.g.
_exitin the example above) should not be used.
**kwargscan't be used at all.
- Changing the function signature later on can be more difficult.
- If you have large numbers of things you need to parameterize, the function definition gets pretty long and ugly.
What's more, you will often have a group of related functions using the same modules, functions or classes. By grouping them in a class, you can implement the parameterization hook once, rather than for every function. You can see an example of this in Crochet (specifically the
Eventloopclass), where the parameterized reactor is used by multiple functions. If the code you need to parameterize is already a method, setting the parameters in
__init__or as a class attribute is even more attractive, requiring only minimal additional complexity.
Update: If you go with this style of parameterization, you still need to assert that in the default case it actual calls the correct function (e.g.
exit_with_result). Probably the nicest way to do so is to use