Working around a lack of 'goto' in python

gabor gabor at z10n.net
Wed Mar 10 17:34:03 CET 2004


On Tue, 2004-03-09 at 22:05, Roy Smith wrote:
> Joe Mason <joe at notcharles.ca> wrote:
> 
> > In article <roy-F325DF.12255009032004 at reader2.panix.com>, Roy Smith wrote:
> > > OK, I'll bite.  What's wrong with exceptions for breaking out of deeply 
> > > nested loops?
> > 
> > Philosophically, the termination condition of a loop isn't an
> > exceptional circumstance.
> 
> The normal flow of control is when you fall out the bottom of the loop 
> when your control test goes false.  Breaking out of the middle is indeed 
> an exceptional circumstance; it is an exception to the normal flow of 
> control.  People tend to think of exceptions as errors, but they don't 
> have to be.
> 
> Now, to be fair, imagine a construct like this:
> 
> try:
>   for i in range (iMax):
>      for j in range (jMax):
>        for k in range (kMax):
>           if thisIsIt (i, j, k):
>              raise IFoundIt
>    print "it's not there"
> except IFoundIt:
>    print "yes it is"
> 
> Presumably the expectation is that the thing you're looking for will 
> exist somewhere in the i,j,k coordinate space.  Falling out the bottom 
> of the loop is now the error condition.  I think this is what you were 
> getting at, and I agree that this turns the whole concept of an 
> exception on its ear.

i think the biggest problem with this exception-stuff is the naming.

the name 'exception' immediately means (for me at least) some kind of
error condition.... 

maybe if they had a different name, it would be easier to use them also
by non-error-conditions.

gabor





More information about the Python-list mailing list