Skip to content Skip to sidebar Skip to footer

Wrap Every Function Or Class Method In A Python Module By Default Without Decorating Every One

I have some code from which I want to get an email whenever it runs into an exception like this. try: f(**kwargs) except Exception as e: # email me the environment I know

Solution 1:

You can introspect modules, so assuming module is the module for which you want to decorate all functions you could do something like this:

import inspect
for name, f in inspect.getmembers(module, inspect.isfunction):
    setattr(module, name, check_error(f))

You could even do this in the current module by using sys.modules[__name__] for module

Post a Comment for "Wrap Every Function Or Class Method In A Python Module By Default Without Decorating Every One"