Working around a lack of 'goto' in python

Stephen Horne steve at ninereeds.fsnet.co.uk
Mon Mar 8 00:29:24 EST 2004


On Sat, 06 Mar 2004 18:18:28 GMT, "Brett" <abc at def.net> wrote:

>BEGIN:
>for (n=0; n < 20; ++n)
>    if (/* some test */) goto BEGIN;
>
>What are the techniques in python for simulating these algorithms without a
>'goto' command?

I just had another thought.

Restarting a loop sounds like error recovery. I _have_ seen something
comparable to this done using recursion...

class Retry (Exception) :
  pass

def widget (a, b) :
  try :
    dostuff ()

    if error :  raise Retry

    dostuff ()

  except Retry :
    #  note - some globals or something needs to be different
    #         or the error will recur, causing an infinite loop.
    widget (a, b)

Or, without the exception...

def widget (a, b) :
  dostuff ()

  if error :
    widget (a, b)
    return

  dostuff ()


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list