| Store | Cart

Working around a lack of 'goto' in python

From: Roy Smith <r...@panix.com>
Sat, 06 Mar 2004 16:16:10 -0500
In article <UBo2c.26879$pg4.12221 at newssvr24.news.prodigy.com>,
 "Brett" <abc at def.net> wrote:

> Two areas where I've found 'goto' two be useful in other languages are in> (untested examples in C++)

Working around the lack of "goto" in a language is kind of like trying 
to figure out how to swim without a piano strapped to your back.  There 
are lots of ways to do it, and even the worst of them is better than the 
alternative.

> (1) deeply nested loops> > for (k=0; k < 10; ++k)> for (j=0; j < 10; ++j)> for (i=0; i <10; ++i)>     if (/* some test */) goto END;> > END: /* continue */;

I've found two general solutions to the breaking out of a deeply nested 
loop.  One possibility is to factor the loop out into its own function, 
and break out of it with a return:

def findTheHiddenThing ():
   for k in range (10):
      for j in range (10):
         for i in range (10):
            if f (i, j, k) == theThingWeWant:
               return

Another is to use an exception:

try:
   for k in range (10):
      for j in range (10):
         for i in range (10):
            if f (i, j, k) == theThingWeWant:
               raise WeFoundIt ("in the last place we looked")
except WeFoundIt:
   whatever

Neither of these ideas are particularly Python specific.  The first 
method works in pretty much any procedural language.  The second only 
works in languages that support exceptions (in some languages, exception 
processing is fairly expensive, so it may not be universally acceptable).

Assuming both methods are equally feasable in your environment, the 
choice of which to use may be driven by style, or one may just be a 
better fit to your problem domain.

On the other hand, if you're doing linear searches of multi-dimensional 
coordinate spaces, that may be a hint that you need to rethink how 
you're solving the problem.  A different data structure may not only 
eliminate the deeply nested loop, but could well be significantly faster 
to execute.

> and (2) repeating a while or for loop from the beginning:> > BEGIN:> for (n=0; n < 20; ++n)>     if (/* some test */) goto BEGIN;

I'm going to go out on a limb on this one and say that if you need to 
begin a loop from the beginning again, something is probably wrong with 
how you're trying to solve the problem.  What you've really written is a 
nested while/for loop.  In C, it should look something like this:

while (1)
   for (n = 0; n < 20; ++n)
      if (some test)
         break

and now you're back to the same sort of "breaking out of a nested loop" 
issue like you had above, with the same likely solutions.

Recent Messages in this Thread
Brett Mar 06, 2004 06:18 pm
Andrew Koenig Mar 06, 2004 06:30 pm
Carmine Noviello Mar 06, 2004 06:41 pm
Jeff Schwaber Mar 06, 2004 06:41 pm
Peter Otten Mar 06, 2004 06:48 pm
Rene Pijlman Mar 06, 2004 06:49 pm
Christian Tismer Mar 06, 2004 08:50 pm
Roy Smith Mar 06, 2004 09:16 pm
Stephen Horne Mar 08, 2004 05:01 am
Dan Bishop Mar 06, 2004 11:23 pm
David M. Cooke Mar 07, 2004 08:49 am
Stephen Horne Mar 07, 2004 06:35 pm
Roy Smith Mar 07, 2004 06:54 pm
Stephen Horne Mar 07, 2004 07:57 pm
Roy Smith Mar 07, 2004 08:49 pm
Stephen Horne Mar 08, 2004 02:54 am
benjamin schollnick Mar 08, 2004 03:38 am
Stephen Horne Mar 08, 2004 05:10 am
Roger Binns Mar 07, 2004 10:33 pm
Roy Smith Mar 08, 2004 02:11 am
Roger Binns Mar 08, 2004 05:09 am
Georgy Mar 08, 2004 09:09 am
Jeff Epler Mar 08, 2004 02:50 pm
Mel Wilson Mar 08, 2004 03:43 pm
Roger Binns Mar 08, 2004 08:43 pm
Donn Cave Mar 08, 2004 10:46 pm
Roger Binns Mar 09, 2004 12:11 am
Stephen Horne Mar 08, 2004 03:30 am
Stephen Horne Mar 08, 2004 05:29 am
Y2KYZFR1 Mar 08, 2004 04:41 pm
Lou Pecora Mar 08, 2004 04:59 pm
Y2KYZFR1 Mar 09, 2004 04:36 pm
Joe Mason Mar 09, 2004 04:57 pm
Roy Smith Mar 09, 2004 05:25 pm
Joe Mason Mar 09, 2004 06:53 pm
Roy Smith Mar 09, 2004 09:05 pm
Roger Binns Mar 10, 2004 04:23 am
Roy Smith Mar 10, 2004 01:52 pm
Isaac To Mar 10, 2004 05:36 am
Jeff Epler Mar 10, 2004 01:05 pm
Isaac To Mar 10, 2004 03:58 pm
gabor Mar 10, 2004 04:34 pm
Donn Cave Mar 10, 2004 05:49 pm
Stephen Horne Mar 11, 2004 03:02 am
Jacek Generowicz Mar 11, 2004 11:40 am
Stephen Horne Mar 11, 2004 02:50 pm
Stephen Horne Mar 09, 2004 09:43 pm
Roy Smith Mar 09, 2004 09:55 pm
Stephen Horne Mar 10, 2004 12:41 am
Isaac To Mar 10, 2004 05:16 am
Christopher A. Craig Mar 08, 2004 07:54 pm
leeg Mar 09, 2004 01:34 am
Stephen Horne Mar 09, 2004 09:52 pm
leeg Mar 10, 2004 02:46 pm
Stephen Horne Mar 10, 2004 04:58 pm
Roger Binns Mar 10, 2004 06:14 pm
David MacQuigg Mar 09, 2004 11:26 pm
Anton Vredegoor Mar 10, 2004 10:11 am
Messages in this thread