[Python-Dev] PEP 380 (yield from a subgenerator) comments

Nick Coghlan ncoghlan at gmail.com
Sat Mar 28 10:34:12 CET 2009


Michele Simionato wrote:
> On Fri, Mar 27, 2009 at 1:33 PM, Jesse Noller <jnoller at gmail.com> wrote:
>> Antoine Pitrou:
>>> As a matter of fact, the people whom this PEP is supposed to benefit haven't
>>> expressed a lot of enthusiasm right now. That's why it looks so academic.
>> That's because most of us who might like this have been patently
>> avoiding this thread.
> 
> I have been avoiding this thread too - even if I have implemented my
> own trampoline as
> everybody else here - because I had nothing to say that was not said
> already here.
> But just to add a data point, let me say that I agree with Eby.
> I am 0+ on the syntax, but please keep the hidden logic simple and
> absolutely do NOT add confusion
> between yield and return. Use yield Return(value) or raise SomeException(value),
> as you like.

I still think raise is out due to the fact that it would trigger
subsequent except clauses. Guido has (sensibly) ruled out raising
StopIteration and complaining if it has value in old code, since there
is too much code which cases StopIteration *without* performing such a
check.

If those two points are accepted as valid, then that leaves the two
options as being:

1. Add a new GeneratorReturn exception that will escape from existing
code that only traps StopIteration. The only real downside of this is
that either "return" and "return None" will mean different things in
generators (unlike functions) or else "return None" will need to be
special cased to raise StopIteration in the calling code rather than
raising GeneratorReturn(None). The latter approach is probably
preferable if this option is chosen - any code for dealing with
"generators as coroutines" is going to have to deal with the possibility
of bare returns and falling off the end of the function anyway, so the
special case really wouldn't be that special.

2. In addition to the "yield from" syntax for delegating to a
subgenerator, also add new syntax for returning values from
subgenerators so that the basic "return X" can continue to trigger
SyntaxError.

Since option 2 would most likely lead to a bikeshed discussion of epic
proportions, I'm currently a fan of option 1 ;)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list