Working around a lack of 'goto' in python

Jeff Schwaber freyley at gmx.net
Sat Mar 6 19:41:39 CET 2004


On Sat, 2004-03-06 at 10:18, Brett wrote:
> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)
> 
> (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 */;

try:
   for k in range(10):
      for j in range(10):
         for i in range(10):
            if "some test":  raise Exception("test")
except:
   pass



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

for n in range(20):
    if "some test": continue

Jeff





More information about the Python-list mailing list