
On Thu, Feb 19, 2009 at 8:16 AM, Greg Falcon <veloso@verylowsodium.com> wrote:
On Thu, Feb 19, 2009 at 5:12 AM, Greg Ewing <greg.ewing@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 try/finally.
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/)