[Python-ideas] Immemorial desire for "do-while"-like construction

Steven D'Aprano steve at pearwood.info
Thu Jul 16 13:26:03 CEST 2009


On Thu, 16 Jul 2009 10:32:01 am Josiah Carlson wrote:
> I use 1/0 instead of True/False because older Pythons loaded numeric
> constants faster than the names True/False (is that still the case?)

From Python 2.6.1:

>>> import dis
>>> dis.dis(compile("while 1: break", "", "exec"))
  1           0 SETUP_LOOP               4 (to 7)
        >>    3 BREAK_LOOP
              4 JUMP_ABSOLUTE            3
        >>    7 LOAD_CONST               0 (None)
             10 RETURN_VALUE
>>> dis.dis(compile("while True: break", "", "exec"))
  1           0 SETUP_LOOP              13 (to 16)
        >>    3 LOAD_NAME                0 (True)
              6 JUMP_IF_FALSE            5 (to 14)
              9 POP_TOP
             10 BREAK_LOOP
             11 JUMP_ABSOLUTE            3
        >>   14 POP_TOP
             15 POP_BLOCK
        >>   16 LOAD_CONST               0 (None)
             19 RETURN_VALUE


True is a constant in Python 3.0.


-- 
Steven D'Aprano



More information about the Python-ideas mailing list