[Python-ideas] "While" suggestion

Guido van Rossum guido at python.org
Fri Jul 4 05:33:22 CEST 2008


On Thu, Jul 3, 2008 at 8:21 PM, Fred Drake <fdrake at acm.org> wrote:
> On Jul 3, 2008, at 10:58 PM, Raymond Hettinger wrote:
>>
>> I co-authored a PEP for a do-while construct and it died solely because we
>> couldn't find a good pythonic syntax.  The proposed while-as construct will
>> neatly solve some of the use cases -- the rest will have to live with what
>> we've got.
>
> I actually like the "do: ... while <expr>: ..." syntax better.

Same here. The while ... as ... syntax introduces a special case that
doesn't handle enough cases to be worth the cost of a special case.
Zen of Python: "Special cases aren't special enough to break the
rules."

To summarize why while...as... doesn't handle enough cases: it only
works if the <setup> can be expressed a single assignment and the
value assigned is also the value to be tested. Like it or not, many
uses of assignment expressions in C take forms like these:

if ((x = <expr>) > 0 && (y = <expr>) < 0) { ... }
while ((p = foo()) != NULL && p->bar) { ... }

If we were to add "while x as y:" in 3.1, I'm sure we'd get pressure
to add "if x as y:" in 3.2, and then "if (x as y) and (p as q):" in
3.3, and so on.

Assignment-in-test: Just Say No.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list