locals() 函数会以字典类型返回当前位置的全部局部变量。
示例代码1:
num = 1
dic = {"name": "dgw"}
print(locals())
print(globals())
运行结果: 【此时globals()和locals()返回结果是一样的】
示例代码2:
num = 1
dic = {"name": "dgw"}
print(locals())
print(globals())
def func(s):
num = 2
print(locals())
print(globals())
func('aa')
运行结果: