First of all a disclaimer:
I don’t want to use a code like this, I am aware it is a bad practice. As well I am not interested in tips on how to improve it, to make it right. What interests me is a theory.
为什么这样的代码在python 3.6中有效:
ls = range(5)
for elem_a in ls:
ls = range(5, 10)
for elem_b in ls:
print(elem_a, elem_b)
我在迭代它时重新分配ls的值.在ls中第一次执行elem_a时,第一次迭代中的ls值是否存储在内存中?
最佳答案 重新分配您正在循环的变量没有任何效果,因为不会为每次迭代重新评估变量.实际上,循环内部循环遍历
iterator,而不是覆盖范围对象.
基本上如果你有这样的循环:
seq = range(5)
for elem in seq:
seq = something_else
Python将其重写为以下内容:
seq = range(5)
loop_iter = iter(seq) # obtain an iterator
while True:
try:
elem = next(loop_iter) # get the next element from the iterator
except StopIteration:
break # the iterator is exhausted, end the loop
# execute the loop body
seq = something_else
这个的关键方面是循环有自己对iter(seq)的引用存储在loop_iter中,所以自然地重新分配seq对循环没有影响.
所有这些都在compound statement documentation中解释:
06002
The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of theexpression_list
.
The suite is then executed once for each item provided by the
iterator, in the order returned by the iterator.