goto in Python

By: Python Documentation Team Viewed: 192 times  Printer Friendly Format    


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.



Most Viewed Articles (in Python )

Latest Articles (in Python)

Comment on this tutorial