[Python-Dev] PEP 377 - allow __enter__() methods to skip the statement body

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Mar 16 00:40:16 CET 2009

Aahz wrote:

> This is pretty much the canonical example showing why control-flow
> exceptions are a Good Thing.  They're a *structured* goto.

I'm wondering whether what we really want is something
that actually *is* a structured goto. Or something like
a very light-weight exception that doesn't carry all the
expensive baggage of tracebacks, isinstance() tests for
matching, etc.

Ruby seems to have something like this. It has a
try/rescue/raise mechanism that works like Python's
try/except/raise, but it also has try/catch/throw as
a less expensive alternative for flow control.

A Python version might look like

   Foo = object() # a token to throw

   catch Foo:

   def do_something():
     throw Foo

Nothing is instantiated -- the token value itself is
thrown -- and the catch clauses compare it by identity
with candidate values.

There is also no traceback carried by the thrown token.

(Although if something is thrown but not caught, an
exception should be raised at the point of the throw
with an appropriate traceback -- implementation of that
is left as an exercise for the reader.)


More information about the Python-Dev mailing list