[Python-ideas] Is this PEP-able? for X in ListY while conditionZ:
Daniel Holth
dholth at gmail.com
Tue Jun 25 15:59:19 CEST 2013
http://docs.python.org/2/library/itertools.html#itertools.takewhile
for item in takewhile(lambda x: x < 5, range(10)):
pass
On Tue, Jun 25, 2013 at 9:35 AM, <jimjhb at aol.com> wrote:
> Syntax:
>
> for X in ListY while conditionZ:
>
> The 'for' loop would proceed as long as conditionZ remains true.
>
> The motivation is to be able to make use of all the great aspects of the
> python 'for' (no indexing or explicit
> end condition check, etc.) and at the same time avoiding a 'break' from the
> 'for'.
>
> (NOTE: Many people are being taught to avoid 'break' and 'continue' at all
> costs, so they instead convert
> the clean 'for' into a less-clean 'while'. Or they just let the 'for' run
> out. You can argue against this teaching
> practice (at least for Python) but that doesn't mean it's not prevalent and
> prevailing.)
>
> [People who avoid the 'break' by functionalizing an inner portion of the
> loop are just kidding themselves and making their own code worse, IMO.
> Takewhile from itertools also works, but that's clumsy and wordy as well.]
>
> I'm not super familiar with CPython, but I'm pretty sure I could get this up
> and working without too much effort.
>
> Please note that I don't feel the answer to this is 'just use break'.
> Programmers are now being taught to avoid 'break' and 'continue' as if they
> were 'goto's. The result (now) is that people are avoiding the 'for' (with
> its GREAT properties) because they can't break out of it.
>
> Comments and Questions welcome.
>
> Thanks.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list