Named code blockes

Steven D. Majewski sdm7g at Virginia.EDU
Sat Apr 14 12:52:08 EDT 2001


On Sat, 14 Apr 2001, Miki Tebeka wrote:

>Alex:
> > In Python, you can code around this problem in various ways (besides
> > the classic ideas based on using state-flags to let the inner loop tell
> the
> > outer one it must terminate).  try/except is a very powerful building
> > block for all sorts of "get out of this nested construct" call, for
> example;
> > it also works across function calls, even recursive ones (its very power
> > and generality can make it less immediately clear than a more local
> > named-break would be, of course).

> If these are code-around then we agree that there is a problem there.
> I agree with these technique, however try/except have a relatively high
> performance impact comparing to a simple implementation of named-break which
> will be a goto in c.

Most of the impact is when you take the exception, and usually,
leaving the loop IS the exceptional condition. 

In fact, setting up the exception is exactly the same code as
setting up a loop:

                case SETUP_LOOP:
                case SETUP_EXCEPT:
                case SETUP_FINALLY:
                        PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() +  oparg,
                                           STACK_LEVEL());
                        continue;



  I wouldn't call it a code-around though -- that just one of the
typical things exceptions were designed for in a structured goto-less 
language. In a more optimized compiled language, there's no reason
reason that a local exception can't be compiled into the machine
language equivalent of a goto, just like while loops are. 

  Python's compiler doesn't do much optimization, and it's dynamic
types for everything including exceptions would make it take a bit
more effort than for a statically typed language. 

-- Steve Majewski






More information about the Python-list mailing list