[Python-Dev] PEP 479: Change StopIteration handling inside generators (original) (raw)

Ron Adam ron3200 at gmail.com
Sun Nov 23 03:48:15 CET 2014


On 11/22/2014 07:06 PM, Chris Angelico wrote:

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 compexpression(): >>> > 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 compexpression()) > >Right, the list.append() should be a yield(expr).

In that case, your second generator expression is entirely redundant; all you want is list(compexpression()). Yes, and that is good. Simplies it even more.

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.

I presume you are asking why do this? And not why the example does that?

There has been a desires expressed, more than a few times, to make comprehensions more like generator expressions in the past. It looks to me that that desire is still true. I also think there has been quite a bit of confusion in these discussions that could be reduced substantially by making Comprehensions work a bit more like generator expressions.

As to why the example does that.. a list constructor iterates over a generator expression in a way that follows the iterator protocol. If you make comprehsionsons work as if they are a generator expression fed to a constructor... then it too should follow the itorator protocol. Do you agree?

>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

Doesn't that patch effect generators and not comprehensions? If so, it wouldn't do what we are talking about here.

Ron



More information about the Python-Dev mailing list