python 返回函数参数名和传入参数值的字典表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 返回函数参数名和传入参数值的字典表相关的知识,希望对你有一定的参考价值。

def foo(f, b):
    c = 3
    # 以下写法错误,locals是会变化的即使使用list(locals())依然无法得到正确的结果
    # [locals.get(i) for i in locals()]
    # 以下2、3通用
    frame = inspect.currentframe()
    args, _, _, value = inspect.getargvalues(frame)
    print({i:value.get(i) for i in args})

foo(1, 2)

以上是关于python 返回函数参数名和传入参数值的字典表的主要内容,如果未能解决你的问题,请参考以下文章