使用Python 3,dis.dis()可以很好地解析包含for
循环语法的字符串:
>>> import dis
>>> dis.dis('for _ in range(10): pass')
1 0 SETUP_LOOP 20 (to 23)
3 LOAD_NAME 0 (range)
6 LOAD_CONST 0 (10)
9 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
12 GET_ITER
>> 13 FOR_ITER 6 (to 22)
16 STORE_NAME 1 (_)
19 JUMP_ABSOLUTE 13
>> 22 POP_BLOCK
>> 23 LOAD_CONST 1 (None)
26 RETURN_VALUE
在Python 2中,它产生了string index out of range
错误:
In [30]: dis.dis('for _ in range(10): pass')
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-30-b7c7c1c77064> in <module>()
----> 1 dis.dis('for _ in range(10): pass')
/usr/lib/python2.7/dis.pyc in dis(x)
43 disassemble(x)
44 elif isinstance(x, str):
---> 45 disassemble_string(x)
46 else:
47 raise TypeError, \
/usr/lib/python2.7/dis.pyc in disassemble_string(code, lasti, varnames, names, constants)
110 def disassemble_string(code, lasti=-1, varnames=None, names=None,
111 constants=None):
--> 112 labels = findlabels(code)
113 n = len(code)
114 i = 0
/usr/lib/python2.7/dis.pyc in findlabels(code)
164 i = i+1
165 if op >= HAVE_ARGUMENT:
--> 166 oparg = ord(code[i]) + ord(code[i+1])*256
167 i = i+2
168 label = -1
IndexError: string index out of range
即使将for
循环包含在常规语法的函数中也无济于事:
def test():
for _ in range(10):
pass
dis.dis('test')
<The same error>
这是否是一个错误或所需行为记录在哪里?
软件版本
$ python2 -V
Python 2.7.9
$ python3 -V
Python 3.4.2
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support/"
BUG_REPORT_URL="https://bugs.debian.org/"