[Python-ideas] Generator syntax hooks?

Brice Parent contact at brice.xyz
Fri Aug 11 08:53:11 EDT 2017


(re-posting here as I first mistakenly answered directly to Terry. Sorry 
about that!)
Le 10/08/17 à 22:03, Terry Reedy a écrit :
> [...]
> result = []
> for x in sequence:
>     if condition:
>         result.append(expression)
>     else:
>         break
>
> which could be written
>
> [expression for x in sequence if condition break]
>
It's what I thought too. Adding a `while` clause here just overly 
complicates the understanding of the comprehension. The `break` keyword 
is already easily understandable and helps to map the comprehension with 
the plain for-loop (I like this mapping for its reverse counterpart, as 
I often start with plain for-loops to rewrite them later to 
comprehensions when it makes sense).
I would probably suggest this instead of Terry's proposal, though:
[expression for x in sequence if condition*or *break]
(maybe it's what you meant?).
I suggest this because it doesn't imply the execution of a statement 
inside the comprehension, but just to continue the evaluation as it's 
always done.
I admit it feels a bit hacky, but maybe just until we get used to it?

-Brice
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170811/c57c137a/attachment-0001.html>


More information about the Python-ideas mailing list