Good python equivalent to C goto

info at orlans-amo.be info at orlans-amo.be
Sun Aug 17 04:20:48 EDT 2008


as an oldtimer, I know that in complex code the goto statement is
still the easiest to code and understand.

I propose this solution using exception.

The string exception is deprecated but is simpler for this example.



# DeprecationWarning: raising a string exception is deprecated

def Goto_is_not_dead(nIn):
    try:
        if (nIn == 1): raise 'Goto_Exit'
        if (nIn == 2): raise 'Goto_Exit'

        print 'Good Input ' + str(nIn)
        raise 'Goto_Exit'

    except 'Goto_Exit':
        print 'any input ' + str(nIn)

if __name__ == '__main__':
    Goto_is_not_dead(2)
    Goto_is_not_dead(3)



More information about the Python-list mailing list