我对python中变量的作用域感到困惑。这里是什么,我试图做一个玩具的例子:for/if循环和python变量的作用域
a = True
enumerated_set = enumerate(['tic','tac','toe'])
for i,j in enumerated_set:
if a == True:
print j
结果我得到的是:
tic
tac
toe
现在
,
print a
回报
`True`
如果我再跑一次
for i,j in enumerated_set:
if a == True:
print j
我没有输出。
我很困惑...因为全球范围内a = True
,为什么在第二个循环中打印没有执行。
我感谢您的帮助。
编辑:在这里我很困惑
y = 'I like this weather'.split()
for item in y:
for i,j in enumerated_set:
if a == True:
print j
也不会产生输出另一个例子....
您每次需要使用时都需要重新创建由'enumerate'创建的生成器。 –