PEP-315 ("do" loop)

Jeremy Fincher tweedgeezer at
Tue Feb 17 19:44:54 CET 2004

Wayne Folta <wfolta at> wrote in message news:<mailman.55.1076984559.31398.python-list at>...
> 2. It is a very general construct, which might be what is called for. 
> But I wonder if most of the time it would be used to accomplish 
> something like:
> while 1:
>      line = sys.stdin.readline()
>      if line == "\n":
>          break

If you're concerned about the "beauty" of such code, then the
appropriate way to rewrite it is with a custom iterator/generator.

def untilBlankLine(fh):
    line = fh.readline()
    while line != '\n':
        yield line
        line = fh.readline()

And then you can use it like so:

for line in untilBlankLine(sys.stdin):

And remember, you can also create custom iterators with the builtin

for line in iter(, '\n'):

So I really see no need for any more complex looping construct.  The
further we get from encouraging the use of while instead of for, the


More information about the Python-list mailing list