3 Answers
3

Short Answer

In Python 2.x, the str type holds raw bytes, so dis assumes that if you pass it a string it is getting compiled bytecode. It tries to disassemble the string you pass it as bytecode and -- purely due to the implementation details of Python bytecode -- succeeds for i in (2,3). Obviously, though, it returns gibberish.

In Python 3.x, the str type is for strings and the bytes types is for raw bytes, so dis can distinguish between compiled bytecode and strings -- and assumes it is getting source code if it gets a string.

Aha! Notice also that the generated bytecode in Python 3.2 is what you would expect ("load i, load (2,3), test for membership, return the result") whereas what you have got in Python 2.7 is gibberish. Clearly, dis is decompiling the string as bytecode in 2.7 but compiling it as Python in 3.2.

And here's the corresponding part of the doc From the Python2 documentation ( docs.python.org/library/dis.html ): >dis.dis([bytesource]) > Disassemble the bytesource object. > bytesource can denote either a module, a class, a method, a function, or a code object whereas in the Python 3 documentation ( docs.python.org/dev/library/dis.html?highlight=dis#dis) it states: >dis.dis(x=None) >Disassemble the x object. x can denote either a module, a class, a method, a function, a code object, a string of source code or a byte sequence of raw bytecode.
–
InkaneMay 6 '12 at 20:41