function - How to save class method's meta data in Python -
i have python class several different methods doing different things, i'd each of them, when runs, save name , argument values, can use them later. 1 way found add each of them:
frame = inspect.currentframe() args, _, _, values = inspect.getargvalues(frame) function_name = inspect.getframeinfo(frame)[2] function_meta = {arg: values[arg] arg in args} self.meta[function_name] = function_meta
this fine, however, i'd prefer keep dry , don't have copy/paste identical snippet every method add.
alternatively, turn separate function save_meta()
, inspect
return data of save_meta()
, instead of function called it, right? there elegant , reusable way of solving this?
you seem dependent on current frame inspect
. suggest
def save_meta(frame): args, _, _, values = inspect.getargvalues(frame) function_name = inspect.getframeinfo(frame)[2] function_meta = {arg: values[arg] arg in args} self.meta[function_name] = function_meta
and call with
save_meta(inspect.currentframe())
Comments
Post a Comment