[Python-ideas] "While" suggestion

Greg Falcon veloso at verylowsodium.com
Thu Jul 3 17:51:23 CEST 2008

On Thu, Jul 3, 2008 at 10:06 AM, Stavros Korokithakis
<stavros at korokithakis.net> wrote:
> while my_file.read(1024) as data:
>   do_something(data)

I dislike this for aesthetic reasons.

One of the nice things about Python is that is reads like executable
psuedocode.  It feels more or less intuitively obvious what the
various looping constructs do.  I'd argue "while foo as bar" is

I realize this syntax proposal is strongly parallel to "with foo as
bar", but "with foo as bar" just seems to read better.  Taken as an
English clause, it gives at least a hint as to what's going on.  I can
imagine an experienced programmer, new to Python, seeing "while foo as
bar" and assuming "as" is some sort of binary operator.

This would be a minor quibble if this proposed syntax solved a
problem.  But as Chris Rebert points out, you can get the same effect
today at the cost of two or three lines extra lines by writing it as a
"while True" loop with a test and break inside.  This form is only
slightly longer, and is immediately obvious to programmers coming from
many other languages.

Greg F

More information about the Python-ideas mailing list