[Python-Dev] Wishlist: dowhile

Phillip J. Eby pje at telecommunity.com
Mon Jun 13 07:25:51 CEST 2005

At 09:01 PM 6/12/2005 -0700, Guido van Rossum wrote:
>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".

Block-copying a file with read() has the same pattern (e.g. in shutil), but 
you can't make it a for loop.

By the way, whatever happened to "and while"?  i.e.:

     while True:
         data = inp.read(blocksize)
     and while data:

It seemed to me this variation of the syntax had some traction at one 
point.  Anyway, most of the times that I'm annoyed by the current while 
loop's limitations are due to the need to write things like the above as:

     while True:
         data = inp.read(blocksize)
         if not data:

Which makes it a bit harder to see the control flow.  Or more precisely, 
something like the 'do/while' or 'while/and while' would make it *easier* 
to see the control flow, since really while isn't *bad*.

I seem to recall that Knuth had something to say about the ideal loop being 
one that allowed you to execute code both before and after the exit 
condition, and that many languages allow you to have code before, or code 
after, but few allow you to do both in the same loop, forcing you to either 
duplicate some code or restructure your approach in order to fit the 
limitation of the language.

More information about the Python-Dev mailing list