I have some boiler plate logic that I want to wrap several functions that have the same optional keyword. Right now it looks like the code below. However, this only handles the case that opt_key is passed as a keyword, as opposed to being passed positionally. One way to solve this would be to know how the argument assignment would be resolved.
Is there some meta function that take a function, args, and kwargs, and returns a dictionary that gives the map from variable names to values? So for below,
meta_function(g,1,2,3)
would return {"x" : 1, "y" : 2, "opt_key" : 3}
and
meta_function(g,1,2,opt_key=3)
would also return {"x" : 1, "y" : 2, "opt_key" : 3}
def g(x,y,opt_key = None):
return something
def h(z,opt_key = None):
return something else
def wrap(f,*args,**kwargs):
if kwargs["opt_key"] is None:
#Do some stuff to get default opt_key
kwargs["opt_key"] = default_opt_key
v = f(args,kwargs)
#Do some stuff to dispose default opt_key
return v
else:
return f(args,kwargs)
wrap(g,x,y,opt_key = None)
wrap(h,x,opt_key = None)
question from:
https://stackoverflow.com/questions/65905885/can-you-determine-from-a-function-args-and-kwargs-how-variables-will-be-assig 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…