question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

Diez B. Roggisch deets at nospam.web.de
Wed Jun 17 05:59:15 EDT 2009


higer wrote:

> Hi,all:
> 
> I'm sorry that I did not make my question clear. What I mean is that
> what the souce code would look like that will be compiled to such
> bytecodes.

>>> import dis
>>> def foo():
...     for i in xrange(10):
...         if i == 5:
...            break
...         if i == 4:
...            continue
...         i *= 100
...
>>> dis.disassemble(foo.func_code)
  2           0 SETUP_LOOP              68 (to 71)
              3 LOAD_GLOBAL              0 (xrange)
              6 LOAD_CONST               1 (10)
              9 CALL_FUNCTION            1
             12 GET_ITER
        >>   13 FOR_ITER                54 (to 70)
             16 STORE_FAST               0 (i)

  3          19 LOAD_FAST                0 (i)
             22 LOAD_CONST               2 (5)
             25 COMPARE_OP               2 (==)
             28 JUMP_IF_FALSE            5 (to 36)
             31 POP_TOP

  4          32 BREAK_LOOP
             33 JUMP_FORWARD             1 (to 37)
        >>   36 POP_TOP

  5     >>   37 LOAD_FAST                0 (i)
             40 LOAD_CONST               3 (4)
             43 COMPARE_OP               2 (==)
             46 JUMP_IF_FALSE            7 (to 56)
             49 POP_TOP

  6          50 JUMP_ABSOLUTE           13
             53 JUMP_FORWARD             1 (to 57)
        >>   56 POP_TOP

  7     >>   57 LOAD_FAST                0 (i)
             60 LOAD_CONST               4 (100)
             63 INPLACE_MULTIPLY
             64 STORE_FAST               0 (i)
             67 JUMP_ABSOLUTE           13
        >>   70 POP_BLOCK
        >>   71 LOAD_CONST               0 (None)
             74 RETURN_VALUE
>>>                                                                   


Pretty much everything with control-structures.

Diez



More information about the Python-list mailing list