GOTO w/ Python?

John Roth johnroth at ameritech.net
Wed Jun 19 16:58:25 EDT 2002


"Angela Williams" <adwilliams76 at hotmail.com> wrote in message
news:3d10ed95 at xpl-sdf1-sec1....
> I'm looking for the equivalent of the GOTO command for Python 2.2.1
running
> on Windows 98. I have programmed very little and am new to Python. I
found
> the following information on the FAQ page but it doesn't make any
sense to
> me.
>
>
> 6.26. Why no goto?
> Actually, you can use exceptions to provide a "structured goto" that
even
> works across function calls. Many feel that exceptions can
conveniently
> emulate all reasonable uses of the "go" or "goto" constructs of C,
Fortran,
> and other languages. For example:
>
>    class label: pass # declare a label
>    try:
>         ...
>         if (condition): raise label() # goto label
>         ...
>    except label: # where to goto
>         pass
>    ...
>
> This doesn't allow you to jump into the middle of a loop, but that's
usually
> considered an abuse of goto anyway. Use sparingly.
>
>
>
> Any and all help would be greatly appreciated.
>
> Thanks, Angela

There's no equivalent of a goto command in Python, for good reason,
which I won't repeat here. Prof. Djikstra did it very well in 1974,
in his letter "Goto Considered Harmful."

If you'd like to post the problem you're trying to solve, someone
will be sure to help you.

The reason for mentioning exceptions in the FAQ is that, for a
long while, exception handling was the sole programming task
where people thought they still needed a goto. That was solved
over a decade ago, with structured exception handling, and Python
simply does it the same as everyone else, only (possibly) better.

John Roth
>
>





More information about the Python-list mailing list