[Python-ideas] Possible PEP 380 tweak
Guido van Rossum
guido at python.org
Sat Oct 30 01:45:28 CEST 2010
On Fri, Oct 29, 2010 at 4:30 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Guido van Rossum wrote:
>
>> I went over that myself in detail and ended up deciding that for
>> "yield-from" nothing should be changed! The expansion in the PEP
>> remains the same.
>
> In that case, the proposal has nothing to do with PEP 380
> and needn't be mentioned in it -- except perhaps to point
> out that using it in the presence of yield-from may
> not produce the expected result.
The connection is that it works well with returning values from
generators, which *is* specified in PEP 380. So I think this does
belong there.
>> 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.)
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list