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

Terry Reedy tjreedy at udel.edu
Thu Jul 16 20:27:49 CEST 2009


Steven D'Aprano wrote:
> 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.

In 3.1
 >>> dis.dis(compile("while True: break", "", "exec"))
   1           0 SETUP_LOOP               4 (to 7)
         >>    3 BREAK_LOOP
               4 JUMP_ABSOLUTE            3
         >>    7 LOAD_CONST               0 (None)
              10 RETURN_VALUE

Same as while 1

tjr




More information about the Python-ideas mailing list