beliavsky wrote: > Goto is useful in breaking out of a nested loop and when there is a > clean-up section of a function that should be executed for various > error conditions.
But much less useful in languages like Python which have exception handling. At rare times I've needed something like for i in xrange: for j in yrange: for k in zrange: if lookup(i,j,k) == target: completely break out of the loop else: raise Exception("not found") print "I have it!", i, j, k There are four common solutions to this problem 1) use a goto statement to break out of the loop for i in xrange: for j in yrange: for k in zrange: if lookup(i,j,k) == target: goto FOUND else: raise Exception("not found") FOUND: print "I have it!", i, j, k 2) wrap things inside of an exception try: for i in xrange: for j in yrange: for k in zrange: if lookup(i,j,k) == target: raise Found else: raise Exception("not found") except Found: pass print "I have it!", i, j, k 3) have a mutiple-level break statement for i in xrange: for j in yrange: for k in zrange: if lookup(i,j,k) == target: break 3 else: raise Exception("not found") print "I have it!", i, j, k 4) introduce a new function, possibly embedded def _search(): for i in xrange: for j in yrange: for k in zrange: if lookup(i,j,k) == target: return i, j, k raise Exception("not found") i,j,k = _search() print "I have it!", i, j, k Both the exception and function definitions solutions can be done now and I don't think the goto solution adds extra clarity or elegance, so there's little gain at the expense of some rather well known pitfalls. > In another newsgroup I once asked "who needs GOTO" and got some good > answers -- the thread can be found by Googling '[EMAIL PROTECTED] > "explicit GOTO"'. Goto's are less dangerous when they are in the > forward direction, to code appearing later. I only found two google hits, both in a Fortran newsgroup. Other posts by you suggest you often program in that language. Fortran doesn't have exceptions, so gotos are the best solution for how to do certain types of error handling. The same holds for C, as you can tell by reading the Python source code. Andrew [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list