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

Raymond Hettinger python at rcn.com
Sat Feb 14 02:31:54 CET 2009

From: "Greg Ewing" <greg.ewing at canterbury.ac.nz>
> The statement
> ::
>     result = yield from expr
> is semantically equivalent to
> ::
>     _i = iter(expr)
>     try:
>         _u = _i.next()
>         while 1:
>             try:
>                 _v = yield _u
>             except Exception, _e:
>                 if hasattr(_i, 'throw'):
>                     _i.throw(_e)
>                 else:
>                     raise
>             else:
>                 if hasattr(_i, 'send'):
>                     _u = _i.send(_v)
>                 else:
>                     _u = _i.next()
>     except StopIteration, _e:
>         _a = _e.args
>         if len(_a) > 0:
>             result = _a[0]
>         else:
>             result = None
>     finally:
>         if hasattr(_i, 'close'):
>             _i.close()

Are there any use cases that warrant all this complexity?
I not yet seen a single piece of real-world code that would
benefit from yield-from having pass-throughs for send/throw/close.
So far, this seems to have been a purely theoretical exercise
what is possible, but it doesn't seem to include investigation as to
whether it is actually useful.  

In the absence of real-world use cases, it might still be helpful
to look at some contrived, hypothetical use cases so we can
see if the super-powered version actually provides a better
solution (is the code more self-evidently correct, is the construct
easy to use and understand, is it awkward to use)?

The proto-pep seems heavy on specification and light on
showing that this is actually something we want to have.
Plenty of folks have shown an interest in a basic version
of yield-every or yield-from, but prior to this protoPEP,
I've never seen any request for or discussion of a version
that does pass-throughs for send/throw/close.


More information about the Python-ideas mailing list