[Python-ideas] Updating PEP 315: do-while loops

Adam Olsen rhamph at gmail.com
Sun Apr 26 09:39:24 CEST 2009

On Sat, Apr 25, 2009 at 7:00 PM, Raymond Hettinger <python at rcn.com> wrote:
> Also, I've punted on the while-cond-with-assignment case that is being
> handled by
> other proposals such as:
>   while f.read(20) as data != '':
>       ...

We can actually already do this, via a little used option of iter():

for data in iter(lambda: f.read(20), ''):

The primary limitation is the need to use lambda (or
functools.partial, but it's no better).  The next limitation is that
even with lambda you can only use an expression, not a statement.

You can bite the bullet and define a function first, but that ends up
being better accomplished with a generator, and in these cases it's
often better still to use a while True: loop.  Back to square one.
I'm not sure all the reorderings are actually more *readable*, rather
than just more appealing to write.

Adam Olsen, aka Rhamphoryncus

More information about the Python-ideas mailing list