locals()---以字典类型返回当前位置的全局变量
'''
Created on 2019年3月20日
locals()
@author: peng
'''
def run(arg):
i = 0
print(locals())
run(2)
打印
这里有两个变量,都被打印出来了
定义names变量,并进行附值,发现在不同位置,打印是不一样的,说明在不同位置,获取的变量不一样
位置一:
def run(arg):
names = locals()
i = 0
print(names)
run(2)
打印
位置二:
def run(arg):
i = 0
names = locals()
print(names)
run(2)
打印:
locals()---只在方法内的全部变量,有效,方法外是无效的
案例
c=10 ,不在run(arg)方法内,就不会被打印出来
c=10
def run(arg):
a=[4, 24, 43];b=[19, 38, 58]
i = 0
names = locals()
print(names)
run(2)
打印:
打印