[Python-Dev] PEP 380 (yield from a subgenerator) comments
Scott Dial
scott+python-dev at scottdial.com
Fri Mar 27 08:28:55 CET 2009
P.J. Eby wrote:
> One remaining quirk or missing piece: ISTM there needs to be a way to
> extract the return value without using a yield-from statement. I mean,
> you could write a utility function like:
>
> def unyield(geniter):
> try:
> while 1: geniter.next()
> except GeneratorReturn as v:
> return v.value
My first thought was to ask why it was not equivalent to say:
x = yield g
x = yield from g
This would seem like a more obvious lack of parallelism to pick on wrt.
return values.
This unyield() operation seems contrived. Never before have you been
able to write a generator that returns a value, why would these suddenly
become common practice? The only place a return value seems useful is
when refactoring a generator and you need to mend having loss of a
shared scope. What other use is there for a return value?
It would seem unfortunate for it to be considered a runtime error since
this would prevent sharing a generator amongst "yield from" and
non-"yield from" use cases. Although, it would be trivial to do:
class K:
...
def _f():
yield 1
return 2 # used internally
def f()
# squelch the runtime error
yield from self._f()
As Greg has said a number of times, we allow functions to return values
with them silently being ignored all the time.
--
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu
More information about the Python-Dev
mailing list