[Python-ideas] Possible PEP 380 tweak

Jacob Holm jh at improva.dk
Sat Oct 30 02:10:11 CEST 2010

On 2010-10-30 01:45, Guido van Rossum wrote:
> On Fri, Oct 29, 2010 at 4:30 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Guido van Rossum wrote:
>>> But since this PEP also specifies "return value" it would be nice if
>>> there was a convenient way to capture this value,
>> As long as you're willing to accept that if the generator
>> you're closing is delegating using yield-from, the return
>> value from the inner generator will get lost.
>> To put it another way, if you design a generator to be
>> used in this way (i.e. its caller using close() to finish
>> it and get a value), you may find it awkward or impossible
>> to later refactor it in certain ways.
> Only if after the refactoring the outer generator would need the
> return value of the interrupted yield-from expression in order to
> compute its return value. I think that's reasonable. (It might be
> possible to tweak the yield-from expansion so that the return value is
> assigned before GeneratorExit is re-raised, but that sounds fragile,
> and doesn't always apply, e.g. if the return value is not assigned to
> a local variable.)

I have earlier proposed a simple change that would at least make the
value available.  Instead of reraising the original GeneratorExit after
calling close on the subgenerator, you just raise a new GeneratorExit
with the returned value as its first argument.  Nick seemed in favor of
this idea.

- Jacob

More information about the Python-ideas mailing list