Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created July 22, 2015 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/f5e772812fcfe228db46 to your computer and use it in GitHub Desktop.
Save ficapy/f5e772812fcfe228db46 to your computer and use it in GitHub Desktop.
返回函数参数名和传入参数值的dict表
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment