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

Guido van Rossum guido at python.org
Thu Feb 19 18:21:01 CET 2009

On Thu, Feb 19, 2009 at 8:16 AM, Greg Falcon <veloso at verylowsodium.com> wrote:
> On Thu, Feb 19, 2009 at 5:12 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>    * ``return expr`` in a generator is equivalent to ``raise
>>       StopIteration(expr)``.
> It seems to me equivalence here might not be what you want.
> This parallel does not exist today between "return" and "raise
> StopIteration()", where the former can't be intercepted and blocked by
> a try/except block, but the latter can.

Technically, 'return' is treated as an uncatchable exception -- but an
exception nevertheless, since you *do* get to intercept it with

> I think it would be confusing
> for a return statement to be swallowed by code intended as an error
> handler.

Only marginally though, since once the generator returns, it *does*
raise StopIteration.

But all in all I agree it would be better to keep the existing return
semantics and only turn it into StopIteration(expr) after all
try/except and try/finally blocks have been left -- IOW at the moment
the frame is being cleared up. That would minimize surprises IMO.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-ideas mailing list