Working around a lack of 'goto' in python

Stephen Horne steve at
Mon Mar 8 06:29:24 CET 2004

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

>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) :

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)

  dostuff ()

Steve Horne

steve at ninereeds dot fsnet dot co dot uk

More information about the Python-list mailing list