[Python-ideas] Revised revised revised PEP on yield-from

Raymond Hettinger python at rcn.com
Tue Feb 17 03:51:32 CET 2009


[Greg Ewing]
>     * Any values that the iterator yields are passed directly to the
>       caller.
> 
>     * Any values sent to the delegating generator using ``send()``
>       are sent directly to the iterator.  (If the iterator does not
>       have a ``send()`` method, values sent in are ignored.)
> 
>     * Calls to the ``throw()`` method of the delegating generator are
>       forwarded to the iterator.  (If the iterator does not have a
>       ``throw()`` method, the thrown-in exception is raised in the
>       delegating generator.)
> 
>     * If the delegating generator's ``close()`` method is called, the
>       iterator is finalised before finalising the delegating generator.
> 
> The value of the ``yield from`` expression is the first argument to the
> ``StopIteration`` exception raised by the iterator when it terminates.
> 
> 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.


Looks like a language construct where only a handful
of python programmers will be able to correctly describe
what it does.

I've only seen requests for the functionality in the first bullet point.
The rest seems like unnecessary complexity -- something that
will take a page in the docs rather than a couple lines.


>                 if hasattr(_i, 'throw'):
>                     _i.throw(_e)
>                 else:
>                     raise

This seems like it is awkwardly trying to cater to two competing needs.
It recognized that the outer generator make have a legitimate need
to catch an exception and that the inner generator might want it too.
Unfortunately, only one can be caught and there is no way to have
both the inner and outer generator/iterator each do their part in
servicing an exception.

Also, am concerned that this slows down the more common case
of _i not having a throw method.

The same thoughts apply to send() and close().  Potentially, both an
inner and outer generator will need a close function but will have no
way of doing both.


Raymond



More information about the Python-ideas mailing list