On 21 November 2014 01:49, MRAB <python@mrabarnett.plus.com> wrote:
On 2014-11-20 14:43, Petr Viktorin wrote:
The "while" expresses intent, "return" caters to low-level mechanics. The "nested compound statements" explanation is already not that good: why does the value come first, not last? For readability*. The difference between function/nested compound statements syntax and comprehension syntax is already so big, and the low-level "while x:"→"if not x: break" translation is so easy, that between the two the readability of "while" should win.
I can see the problem with 'while': if there are multiple 'for' parts, they are equivalent to nested 'for' loops, so you might assume that a 'while' part would also be equivalent to a nested 'while' loop, whereas it would, in fact, be controlling the preceding 'for' part.
In other words, it would be:
for x in range(10) while x < 5: ....
but could be seen as:
for x in range(10): while x < 5:
PEP 3142 was the last attempt at asking this question - Guido isn't keen on the idea, so he rejected it the last time we did a pass through the PEPs that weren't being actively worked on. So while the odds still aren't good, a proposal that kept the statement and expression forms consistent might still have some chance, by proposing that the statement example above: for x in range(10) while x < 5: .... Be equivalent to: for x in range(10): if not x < 5: break .... That is, there'd only be one loop, rather than two. At that point, a "while" clause in a comprehension or generator expression would be part of that enhanced syntax, rather than a standalone while loop. That does also suggest another possible alternative to the "or stop()" trick - allow *break* as an expression. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia