如何获取函数/方法的调用参数的值?
它是一个调试工具,它会在这样的场景中使用:
import inspect
def getCallParameter():
stack = inspect.stack()
outer = stack[1] #This is an example, I have a smarter way of finding the right frame
frame = outer.frame
print("") #print at least a dict of the value of the parameters, and at best, give also wich parameter are passed explicitely (e.g. with f(2, 4) an input of this kind : "a=2, b=4, c=5(default)")
def f(a, b=4, c=5):
a+=b+c
getCallParameters()
return a
注意:我知道inspect.formatargsvalues()
但它不符合我的要求,因为在f(2,4)
示例中,它会打印“a = 11,b = 4,c = 5)”
我想的是在外部框架上观察传递的值。如果我没有得到传递对象的原始状态,这不是问题,只要我得到最初绑定到变量参数的对象。例子 :
# with f(4)
def f(a, b=4, c=[])
c.append(5)
getCallParameters() # a=4, b=4, c=[5] is ok even if I would have preferred c=[]
c = [4]
getCallParameters() # here, I exepect c=[5] or c=[]