[Python-ideas] Proto-PEP on a 'yield from' statement

Antoine Pitrou solipsis at pitrou.net
Fri Feb 13 01:13:59 CET 2009


Greg Ewing <greg.ewing at ...> writes:
>> Additionally, generators will be allowed to execute a ``return``
> statement with a value, and that value will be passed as an argument
> to the ``StopIteration`` exception.

What is the use of it?
The problem I can see is that in normal iteration forms (e.g. a "for" loop), the
argument to StopIteration is ignored. Therefore, a generator executing such a
return statement and expecting the caller to use the return value wouldn't be
usable in normal iteration contexts.
> ::
>> result = yield from iterator
>> is semantically equivalent to
>> ::
>> _i = iterator
> try:
> _v = _i.next()
> while 1:
> if hasattr(_i, 'send'):
> _v = _i.send(_v)
> else:
> _v = _i.next()
> except StopIteration, _e:
> _a = _e.args
> if len(_a) > 0:
> result = _a[0]
> else:
> result = None

There seems to lack at least a "yield" statement in this snippet.
Also, why doesn't it call iter() first? Does it mean one couldn't write e.g. 
"yield from my_list"?
Besides, the idea of getting the "result" from the /inner/ generator goes
against the current semantics of "result = yield value", where the result comes
from the /outer/ calling routine.
Regards
Antoine.


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /