Python的locals()函数会以dict类型返回当前位置的全部局部变量。
示例代码:
def func():
arg_a, arg_b = 'a', 'b'
def func_a():
pass
def func_b():
pass
def print_value():
print(arg_a, arg_b)
return locals()
if __name__ == '__main__':
args = func()
print(type(args))
print(args)
运行结果:
{'func_a': <function func.<locals>.func_a at 0x000002758AEE9950>, 'arg_a': 'a', 'arg_b': 'b', 'func_b': <function func.<locals>.func_b at 0x000002758B178EA0>, 'print_value': <function func.<locals>.print_value at 0x000002758B178E18>}