Created
January 14, 2011 08:08
-
-
Save zed/779350 to your computer and use it in GitHub Desktop.
`while True` vs. `while 1` on Python 2.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> def loop(): | |
| ... total = 0 | |
| ... while 1: | |
| ... total += 1 | |
| ... | |
| >>> import dis | |
| >>> dis.dis(loop) | |
| 3 0 LOAD_CONST 1 (0) | |
| 3 STORE_FAST 0 (total) | |
| 4 6 SETUP_LOOP 13 (to 22) | |
| 5 >> 9 LOAD_FAST 0 (total) | |
| 12 LOAD_CONST 2 (1) | |
| 15 INPLACE_ADD | |
| 16 STORE_FAST 0 (total) | |
| 19 JUMP_ABSOLUTE 9 | |
| >> 22 LOAD_CONST 0 (None) | |
| 25 RETURN_VALUE | |
| >>> def loop_true(): | |
| ... total = 0 | |
| ... while True: | |
| ... total += 1 | |
| ... | |
| ... | |
| ... | |
| >>> import dis | |
| >>> dis.dis(loop_true) | |
| 3 0 LOAD_CONST 1 (0) | |
| 3 STORE_FAST 0 (total) | |
| 4 6 SETUP_LOOP 22 (to 31) | |
| >> 9 LOAD_GLOBAL 0 (True) | |
| 12 JUMP_IF_FALSE 14 (to 29) | |
| 15 POP_TOP | |
| 5 16 LOAD_FAST 0 (total) | |
| 19 LOAD_CONST 2 (1) | |
| 22 INPLACE_ADD | |
| 23 STORE_FAST 0 (total) | |
| 26 JUMP_ABSOLUTE 9 | |
| >> 29 POP_TOP | |
| 30 POP_BLOCK | |
| >> 31 LOAD_CONST 0 (None) | |
| 34 RETURN_VALUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment