The "loop and a half"

Paul Moore p.f.moore at gmail.com
Wed Oct 4 10:01:55 EDT 2017


On 4 October 2017 at 14:02, Robin Becker <robin at reportlab.com> wrote:
> On 04/10/2017 11:57, Rhodri James wrote:
>>
>> On 04/10/17 10:01, Robin Becker wrote:
>>>
>>> Given the prevalence of the loop and a half idea in python I wonder why
>>> we don't have a "do" or "loop" statement to start loops without a test.
>>
>>
>> See PEP 315.  Guido's rejection note is here:
>> https://mail.python.org/pipermail/python-ideas/2013-June/021610.html
>>
> seems fair enough; I suppose the cost is negligible or perhaps there's
> peephole optimization for this common case.

There is:

>>> def f():
...     while True:
...         pass
...
>>> import dis
>>> dis.dis(f)
  2           0 SETUP_LOOP               4 (to 6)

  3     >>    2 JUMP_ABSOLUTE            2
              4 POP_BLOCK
        >>    6 LOAD_CONST               0 (None)
              8 RETURN_VALUE

Paul



More information about the Python-list mailing list