GoTo considered missing

Terry Hancock hancock at anansispaceworks.com
Tue Jul 2 09:42:41 EDT 2002


From: "Harvey Frey" <hsfrey at earthlink.net>
>     OK. I'm a spaghetti-coder from way back, new to Python.
> I didn't even realize there was no "goto" till I tried to rewrite a big C
> program in Python.
> 
>     Breaks and Continues are fine for single loops, but:
> What is the approved Python method for getting from the bottom of a set of
> nested loops to the top?
> 
>     Maybe I could rewrite the big loop with function calls, but I'd be
> knee-deep in recursion.

You should probably try to use an exception in this
case.

class ImDone(Exception):
	# ... I don't remember this bit because I remember
	# the now-deprecated exceptions-are-strings syntax
	pass

try:
   loop ...
      loop ...
         loop ...
            loop ...
                if
special-reason-I-need-to-bail-out-like-Im-finished-early:
                     raise ImDone
except ImDone:
    # stuff I need to do after finishing

This is pretty similar to the rare cases in C when "goto"
is considered sound programming style.  Exceptions are
good for more than error-handling!

Cheers,
Terry

-- 
------------------------------------------------------
Terry Hancock
hancock at anansispaceworks.com       
Anansi Spaceworks                 
http://www.anansispaceworks.com 
P.O. Box 60583                     
Pasadena, CA 91116-6583
------------------------------------------------------





More information about the Python-list mailing list