Previous proposals to make for-loop variables local to
the loop have stumbled on the problem of existing code
that relies on the loop variable keeping its value
after exiting the loop, and it seems that this is
regarded as a desirable feature.
如果序列为空,则循环根本不会为变量赋值:
>>> for x in []:
pass
>>> print(x)
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
print(x)
NameError: name 'x' is not defined
见For-loop variable scope
如果序列为空,则循环根本不会为变量赋值:
相关问题 更多 >
编程相关推荐