[Python-ideas] "Iteration stopping" syntax [Was: Is this PEP-able? for X in ListY while conditionZ:]

Ron Adam ron3200 at gmail.com
Sun Jun 30 09:01:47 CEST 2013


On 06/30/2013 12:45 AM, Nick Coghlan wrote:
> You can't get the same semantics for other comprehensions without
> introducing the yield/return distinction, which is astonishingly slow
> by comparison (as you need to suspend and resume the generator frame
> on each iteration). The overhead of that is only worth it when the
> cost of having the entire result in memory at the same time is
> prohibitive.

It seems to me that if we were to rewrite comprehensions as generators 
yielding into the container. the generator would be private.  It can't be 
taken out and reused, never needs to be suspended, and always yields to the 
same container.

Because it is private, it can be altered to make it more efficient by 
removing the unneeded parts and/or substituting alternative byte code that 
does the same thing in a faster way.

Wouldn't they then be equivalent to the current comprehensions with the 
generator exception handling added?

Cheers,
    Ron





More information about the Python-ideas mailing list