GOTO w/ Python?

Robert Amesz sheershion at mailexpire.com
Fri Jun 21 09:30:50 EDT 2002


Angela Williams wrote:

> 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.

Pleas don't go looking for GOTOs. If you find yourself needing those, 
in 99.9% of the cases there's something not quite right with your code. 
Usually, by splitting a large function/method into several smaller ones 
the problem will go away by itself. The resulting code will also be 
clearer and more maintainable.

(If after that you're still having trouble, post a page or so of your 
code here. This is a helpful group, and I'm sure somebody will give you 
some points on how to improve your code.)

The most goto-like contructs in Python are 'return' (which exits from a 
function/method before the end is reached) and 'break' (which exits a 
loop, continuing with statement followin that loop).

In a pinch you can indeed raise some kind of exception, but you really 
should use those for things like errors.


Robert Amesz



More information about the Python-list mailing list