I have a Python class with several different methods doing different things, but I'd like each of them, when it runs, to save its name and argument values, so that I can use them later. One way I found is to add something like this to each of them:
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
function_name = inspect.getframeinfo(frame)[2]
function_meta = {arg: values[arg] for arg in args}
self.meta[function_name] = function_meta
This is fine, however, I'd prefer to keep it DRY and don't have to copy/paste this identical snippet to every method I add.
Alternatively, I could turn it into a separate function save_meta(), but then inspect will return the data of save_meta(), instead of the function that called it, right? Is there any elegant and reusable way of solving this?