python中locals函数用法详解

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')

运行结果:

python中locals函数用法详解_第1张图片

你可能感兴趣的:(python,python,locals)