[Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]
Shane Green
shane at umbrellacode.com
Sat Jun 29 03:50:42 CEST 2013
Yes, but it only works for generator expressions and not comprehensions. My opinion of that workaround is that it’s also a step backward in terms of readability. I suspect
if i < 50 else stop() would probably also work, since it throws an exception. That’s better, IMHO.
On Jun 28, 2013, at 6:38 PM, Andrew Carter <acarter at g.hmc.edu> wrote:
> Digging through the archives (with a quick google search) http://mail.python.org/pipermail/python-ideas/2013-January/019051.html, if you really want an expression it seems you can just do
>
> def stop():
> raise StopIteration
> list(i for i in range(100) if i < 50 or stop())
>
> it seems to me that this would provide syntax that doesn't require lambdas.
>
>
>
>
>
>
> On Fri, Jun 28, 2013 at 4:50 PM, Alexander Belopolsky <alexander.belopolsky at gmail.com> wrote:
>
>
>
> On Fri, Jun 28, 2013 at 7:38 PM, Shane Green <shane at umbrellacode.com> wrote:
> ..
> [x until condition for x in l ...] or
> [x for x in l until condition]
>
> Just to throw in one more variation:
>
> [expr for item in iterable break if condition]
>
> (inversion of "if" and "break"reinforces the idea that we are dealing with an expression rather than a statement - compare with "a if cond else b")
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130628/c6cc4faa/attachment.html>
More information about the Python-ideas
mailing list