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.
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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。