[Python-ideas] Special keyword denoting an infinite loop

Ron Adam ron3200 at gmail.com
Tue Jul 1 02:16:38 CEST 2014



On 06/30/2014 01:20 PM, Steven D'Aprano wrote:
> On Mon, Jun 30, 2014 at 01:24:37PM -0400, random832 at fastmail.us wrote:
>> On Sat, Jun 28, 2014, at 06:05, Stefan Behnel wrote:
>>> Adding a new keyword needs very serious reasoning, and that's a good
>>> thing.
> [...]
>> What about _just_ "while:" or "for:"?
>
> Why bother? Is there anything you can do with a bare "while:" that you
> can't do with "while True:"? If not, what's the point?

It looks like (in python3) "while 1:", "while True:", and while with a 
string, generates the same byte code.  Just a bare SETUP_LOOP.  Which would 
be the exact same as "while:" would.  So no, it wouldn't make a bit of 
difference other than saving a few key strokes in the source code.

 >>> def L():
...    while True:
...        break
...
 >>> L()
 >>> dis(L)
   2           0 SETUP_LOOP               4 (to 7)

   3     >>    3 BREAK_LOOP
               4 JUMP_ABSOLUTE            3
         >>    7 LOAD_CONST               0 (None)
              10 RETURN_VALUE


 >>> def LL():
...    while 1:
...        break
...
 >>> dis(LL)
   2           0 SETUP_LOOP               4 (to 7)

   3     >>    3 BREAK_LOOP
               4 JUMP_ABSOLUTE            3
         >>    7 LOAD_CONST               0 (None)
              10 RETURN_VALUE


 >>> def LLL():
...     while "looping":
...         break
...
 >>> dis(LLL)
   2           0 SETUP_LOOP               4 (to 7)

   3     >>    3 BREAK_LOOP
               4 JUMP_ABSOLUTE            3
         >>    7 LOAD_CONST               0 (None)
              10 RETURN_VALUE


Cheers, Ron



More information about the Python-ideas mailing list