什么
dis告诉我们:
Python 3.4.1 (default, May 19 2014, 13:10:29)[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from dis import dis>>> dis("""x = [1,2,3,4,5]... for x in x:... print(x)... print(x)""") 10 LOAD_ConST 0 (1) 3 LOAD_ConST 1 (2) 6 LOAD_ConST 2 (3) 9 LOAD_ConST 3 (4) 12 LOAD_ConST 4 (5) 15 BUILD_LIST 5 18 STORE_NAME 0 (x) 2 21 SETUP_LOOP 24 (to 48) 24 LOAD_NAME 0 (x) 27 GET_ITER >> 28 FOR_ITER 16 (to 47) 31 STORE_NAME 0 (x) 3 34 LOAD_NAME 1 (print) 37 LOAD_NAME 0 (x) 40 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 43 POP_TOP 44 JUMP_ABSOLUTE28 >> 47 POP_BLOCK 4 >> 48 LOAD_NAME 1 (print) 51 LOAD_NAME 0 (x) 54 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 57 POP_TOP 58 LOAD_ConST 5 (None) 61 RETURN_VALUE关键位是第2部分和第3部分-我们从
x(
24 LOAD_NAME 0 (x))中加载值,然后获得其迭代器(
27GET_ITER)并开始对其进行迭代(
28 FOR_ITER)。Python 再也不会返回以再次加载迭代器 。
旁白:
这将没有任何意义的话,因为它已经拥有了迭代器,并作为作者Abhijit在他的回答指出,Python的规范的第7.3实际上需要这种行为)。
当名称
x被覆盖以指向列表中以前称为
xPython的每个值时,找到迭代器不会有任何问题,因为它无需
x再次查看名称即可完成迭代协议。



