This is an old copy of the Python FAQ. The information here may be outdated.

Why is there no goto?

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.

(For serious abuse, see this goto implementation. This uses the sys.settrace debugger hook to emulate not only goto but also comefrom. Please do not use in real code!)

CATEGORY: general

CATEGORY: design

 

A Django site. rendered by a django application. hosted by webfaction.