Loop until condition is true

Konstantin Veretennicov kveretennicov at gmail.com
Tue Jun 21 10:28:32 EDT 2005


On 6/21/05, Magnus Lycka <lycka at carmen.se> wrote:
> I don't know anything about the Python compiler internals,
> but it doesn't seem very hard to identify simple literals following
> while and if, and to skip the runtime test. (Perhaps it's done
> already?)

True doesn't seem to be a literal, it is looked up by name and can be
rebound to, say, 0 anytime:

>>> import dis
>>> o = compile('while True: pass', '<string>', 'exec')
>>> dis.dis(o)
  1           0 SETUP_LOOP              12 (to 15)
        >>    3 LOAD_NAME                0 (True)
              6 JUMP_IF_FALSE            4 (to 13)
              9 POP_TOP
             10 JUMP_ABSOLUTE            3
             ...

OTOH,

>>> p = compile('while 1: pass', '<string>', 'exec')
>>> dis.dis(p)
  1           0 SETUP_LOOP               5 (to 8)
        >>    3 JUMP_ABSOLUTE            3
             ...

- kv



More information about the Python-list mailing list