The go to in Python or the go to statement in programming was actually started around as a joke. Though, what started as a joke is now among the most essential functions used by almost each and every programmer across the world.

We know that Python is the most simple and go to programming language in the world. It is also one of the most preferred and used language in the world. Everyone, whether a beginner or an expert, all and sundry make use of Python, and one of the most unnoticed statement in Python is the goto statement. In this tutorial, we will see and learn about what is the goto statement and how to use go to in Python in a very subtle way.

The Goto Statement — An Overview

A goto statement is so named because this piece of code gives your code an unconditional jump through the goto statement to a stated statement that’s marked with the goto statement. Discussing it in layman terms, it is generally in use whenever a programmer likes to skip some specific number of functions or statements.

Even though the programmers highly prefer the go to statement, the go to statement lags behind just in case of auditing purposes. It is to be realized that it becomes difficult for the programmers to change any program content and also to locate the precise destination of the goto statement is tedious. It’s due to go to that the code execution jumps from one function to a different conveniently.

Basic Syntax of Go to in Python

# go to in python
goto label; 
....
....
Label:

Label:
....
....
goto label;

### Note : The label is just a sample word and it should be replaced the appropriate text or statement.
# For example: goto return;

The goto statements’ iterations

Another statement that works the same way as goto is the comefrom statement. Though, the comefrom statement has an opposite meaning to the goto statement. In reality, both goto and comefrom statements provide great flexibility and scalability to a Python program. This allows a program to regulate the mechanism of the program flow. It also holds the accessibility to regulate the idioms flow.

A thing to note, as always in any Python program is we need to import the goto and comefrom statement first within the main library. Like :

# importing goto and comefrom in the main library

from goto import goto, comefrom, label

When one intends to use go to in Python, one basically is asking the interpreter to execute another line of statements instead of the present statement. The target statement which you want the interpreter to execute at that instant needs to be noted in the “label” section. Example label .thelabel


A comefrom statement states that the command is coming from somewhere. That is it instantiates the command to come from another instance and not go to some other line of code.

Syntax :

Label .somewhere
comefrom .somewhere

Comefrom is a very critical factor and is used as a debugging aid during programming.

Computed goto in Python

One of the most usual variation of the goto statement in Python is the computed goto statement. In this, one needs to just predefine the python index at the beginning of the code and subsequently refer to it by using a hashtag.

Let’s inspect an example for this :

i = calculateLabelName()
Goto *i

Note: The value of ‘i’ in the statement above must not include the prefixed (.)dot as aforementioned in the example just before this code block.

Are there any restrictions within the goto statement in Python?

Python also has its number of restrictions on comefrom and goto statement as other lines of codes and coding platforms have. Here are those limitations :

  • Programmers can’t use both statements at the same instant to leap between modules or functions.
  • Jumping into the ultimate clause or the center of any loop isn’t possible always.
  • Both or any of these statements won’t be useful to jump into an exception line, because it can’t be found in the first place.

Example :

from goto import goto, label
    for x in range(1, 10):
    for y in range(1, 20):
    for z in range(1, 30):
    print x,y,z
    if z == 3:
       goto .end
       label .end
   print "Finished"


The above example is used to break free from any loop that may be nested however deeply.

Cleaning up after any task fails

# Take these as real-worker functions.
from goto import goto, label
   def settingUp(): 
       print "settingUp"
   def doPrimaryTask(): 
        print 0; 
        return True
   def doSecondaryTask(): 
        print 1; 
        return True
   def doThirdTask(): 
        print 2; 
        return False # It pretends to fail.
   def doFourthTask(): 
        print 3; 
        return True
   def cleanUp(): 
        print "cleanUp"
#  This function allows the third task to cleanup
def bigFunction3():
    settingUp()
    if not doPrimaryTask():
       goto .cleanup
    if not doSecondaryTask():
       goto .cleanup
    if not doThirdTask():
       goto .cleanup
    if not doFourthTask():
       goto .cleanup
    label .cleanup
    cleanUp()
bigFunction3()
print "bigFunction3 done"

The goto statement in Python is thus, a very helpful command and one that is brought to use for both auditing and debugging purposes. Albeit they’re under-utilize in daily programming, but, using a go to statement often provides you with very amazing results.

Some Miscellaneous Program Examples

# Example 1: Using a computed goto:
from goto import goto, label

label .getinput
i = raw_input("Enter either 'a', 'b' or 'c', or any other letter to quit: ")
if i in ('a', 'b', 'c'):
    goto *i
else:
    goto .quit

label .a
print "You typed 'a'"
goto .getinput

label .b
print "You typed 'b'"
goto .getinput

label .c
print "You typed 'c'"
goto .getinput

label .quit
print "Finished\n"

# Example 2: Restarting a loop:
from goto import goto, label
label .start
for i in range(1, 5):
    print i
    if i == 3:
        try:
            output = message
        except NameError:
            print "Oops!  Start again please."
            message = "Hello world"
            goto .start
print output, "\n"

# Exception Program
# Example 3: When a label goes missing
from goto import goto, label
label .isreal
goto .notreal      # Raises a MissingLabelError exception.

++ SUMMING UP ++

So, we now know what is go to in Python, how to use the goto statement. We have seen all this and also the implementation of the goto statement.

Go to in Python

Thus, through this article, you shall have an idea about the general steps involving use of go to in Python. By and through this article, thus, I suppose I have made myself pretty clear. But, in case, you still have some doubts lingering. Then, please do write to me in the comments section and I am as always, ever-ready to help you. And, also solve your many queries and problems.

Until then bidding you Good-Bye !!! Ok, wait ….. before you go, you may check out my various other posts. Also, for the simple reason, that is, to enhance your knowledge on various other topics of importance. Also, where ??? Here…… And, if u still want more as in “Dil Mange More” then visit.