[Python-ideas] Revised^4 PEP on yield-from

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Feb 19 21:48:15 CET 2009

Antoine Pitrou wrote:

> Why not a dedicated exception (e.g. GeneratorReturn) instead?
> Two advantages to doing so:
> * people mistakingly doing a "for" loop over such a generator would be
>   reminded that they are missing something (the return value)

You don't get an warning that you are "missing something"
if you ignore the return value from an ordinary function
call, so I don't find this argument very convincing.

> * you could take advantage of existing iterator-consuming features (e.g.
>   "yield from map(str, innergenerator())"), since they would just forward
>   the exception instead of silencing it

You also don't get ordinary return values of functions
that you call forwarded to your caller, and I don't see
how it would be any more useful to do so here.

There is one possible reason it might be useful, and
that's if you want to catch a StopIteration that may
or may not have a value attached, i.e.

     # some iteration thing
   except GeneratorReturn, e:
     result = e.args[0]
   except StopIteration:
     result = None

However, that would be better addressed by enhancing
StopIteration with a 'value' attribute that is None
if it wasn't given an argument.


More information about the Python-ideas mailing list