0

In the following Python bytecode, I would like to understand why the value of i is not incremented just before JUMP_ABSOLUTE ?

>>> import dis
>>> dis.dis("""for i in range(4):
...     print(i)
... """)
  1           0 SETUP_LOOP              24 (to 26)
              2 LOAD_NAME                0 (range)
              4 LOAD_CONST               0 (4)
              6 CALL_FUNCTION            1
              8 GET_ITER
        >>   10 FOR_ITER                12 (to 24)
             12 STORE_NAME               1 (i)

  2          14 LOAD_NAME                2 (print)
             16 LOAD_NAME                1 (i)
             18 CALL_FUNCTION            1
             20 POP_TOP
             22 JUMP_ABSOLUTE           10
        >>   24 POP_BLOCK
        >>   26 LOAD_CONST               1 (None)
             28 RETURN_VALUE
3
  • 2
    What is your own analysis of the byte-code? And why do you need it? What is the problem you need to solve by this analysis? Where did you get the byte-code from? Commented Oct 24, 2019 at 9:01
  • And to help you understand more about what's happening, you should probably read more about the range class. Commented Oct 24, 2019 at 9:02
  • 1
    docs.python.org/3.5/library/dis.html#bytecodes Commented Oct 24, 2019 at 9:13

1 Answer 1

1

The evolution of i is simply done at the line 10 by calling the magic method __next__ of range.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.