[Python-ideas] Anaphoric if

Nick Coghlan ncoghlan at gmail.com
Sat Apr 24 10:00:53 CEST 2010


Raymond Hettinger wrote:
> Yes, this has come up before.
> FWIW, I'm +1 on the idea,
> especially if it can be also applied to while-loops.
> It looks very readable

Agreed, of the sundry embedded assignment proposals that come up
semi-regularly, adding "as" clauses to if and while statements is the
most viable (although possibly still not a good idea).

Any such proposal would be best promoted by mining existing code (such
as the standard library) for examples that are improved by the new
syntax. The while loop case is actually probably stronger, since it can
avoid code repetition in calculating and saving the loop condition once
before the start of the loop, and then recalculating it again at the end
of the loop.

Note that both decorators and the with statement were successful in
large part due to the way that they brought visually distant code (after
a function definition, after a try block) up to a more semantically
appropriate location (before the function definition line, replacing the
opening line of the try block).

Any discussion of an "as" clause for while statements should at least
mention PEP 315 (adding a "do" block to while loops), as that has a
similar motivation for avoiding code duplication.

> We do have a language moratorium in effect,
> so your odds of success are slim.

Yes, even if a good proposal is put together for this, it won't be
seriously considered until the moratorium is over (which is still years
away).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list