[Python-Dev] Wishlist: dowhile
Guido van Rossum
gvanrossum at gmail.com
Mon Jun 13 06:01:42 CEST 2005
On 6/12/05, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Raymond Hettinger wrote:
> > More than case-statement semantics or PEP343, I wish for a dowhile
> > statement.
> >
> > The most straight-forward way is to put the conditional expression at
> > the beginning of the block with the understanding that a dowhile keyword
> > will evaluate the condition only after the block runs
>
> Out-of-order code execution rarely counts as 'straightforward' ;)
Right. Millions of years of programming language design have shown us
how to write a loop with the condition tested at the end -- the
condition is written after the loop body.
> With PEP 315, a do-while loop would look like:
>
> do:
> <body>
> while <cond>:
> pass
>
> But then, I'm reasonably happy with the 'break out of an infinite
> loop' approach, so *shrug*.
Amen.
If we have to do this, PEP 315 has my +0.
It is Pythonically minimal and the motivation rings true: I've often
written code like this in the past:
line = f.readline()
while line:
<do something>
line = f.readline()
But these days we do that using "for line in f".
I wonder if other similar use cases can't be rewritten using better iterators?
Maybe Raymond can show us some motivating use cases.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list