[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:

Stephen J. Turnbull stephen at xemacs.org
Tue Jun 25 21:07:44 CEST 2013


jimjhb at aol.com writes:

 > Main issue seems to be that programmers "shouldn't" shy away from
 > 'break' (I can kind of see the argument both ways) but they do.

I guess I just hang around in the wrong company, because I don't see
it.  Where can I find these programmers who shy away from these
constructs?

 > I think for deserves a 'while' because it is really sparing in its
 > implementation (no index, no explicit bounds check) and so deserves
 > a clean way to prematurely terminate the loop if needed.

But this idea lacks coherence, in that 'break' statement syntax is far
more expressive than a 'while' clause in the 'for' statement can be.
The 'while' condition needs to satisfy expression syntax and be a
function of the loop variable.  But 'break' can be used at the top of
the loop, bottom, or elsewhere while the new syntax can only implement
one of those.  And the 'break' condition need not be a function of the
loop variable.

On the other hand, in list comprehensions and generator expressions,
the syntactic and functional restrictions on the condition need to be
satisfied anyway.

By the way:

 > David Mertz writes:

 >> attendees = {guest:guest.plus_N for guest in waiting_list while
 >> not room_full()}
 >>
 >> This would actually produce the same result as:
 >>
 >> attendees = {guest:guest.plus_N for guest in waiting_list if not
 >> room_full()}

Not necessarily.  waiting_list might be an infinite iterator.

Steve


More information about the Python-ideas mailing list