[Python-Dev] PEP 479: Change StopIteration handling inside generators

Chris Angelico rosuav at gmail.com
Sun Nov 23 02:06:18 CET 2014


On Sun, Nov 23, 2014 at 11:51 AM, Ron Adam <ron3200 at gmail.com> wrote:
>
>
> On 11/22/2014 06:20 PM, Chris Angelico wrote:
>>
>> Hmmm, there's no such thing as tuple comprehensions.
>
> Just didn't think it through quite well enough.  But you are correct, that
> would be a generator expression.
>
> One less case to worry about. :-)

Ah right, no probs.

>>> >And it would be changed to this...
>>> >
>>> >     def comp_expression():
>>> >         for x in itr:          # StopIteration caught here.
>>> >             if cond:
>>> >                list.append(expr)
>>> >
>>> >     # StopIteration from cond and expr caught here.
>>> >     lst = list(x for x in comp_expression())
>
> Right, the list.append() should be a yield(expr).

In that case, your second generator expression is entirely redundant;
all you want is list(comp_expression()). But the example doesn't say
*why* this version should terminate on a StopIteration raised by expr,
when the statement form would print an exception traceback.

> The real question is how much breakage would such a change make?  That will
> probably need a patch in order to test it out.

There's one attached here:

http://bugs.python.org/issue22906

It doesn't create a __future__ directive, just applies the change globally.

ChrisA


More information about the Python-Dev mailing list