Is there a goto
or any equivalent in Python to be able to jump to a specific line of code?
-
21[import goto](http://entrian.com/goto/) – wim Nov 13 '13 at 23:21
-
29A friend of mine implemented `goto` in Python when he was translating some Fortran code to Python. He hated himself for it. – Cody Piersall May 26 '14 at 02:46
-
7https://github.com/cdjc/goto (it's *much* faster than the entrian implementation) – cdjc Aug 19 '14 at 20:30
-
2In the context of "goto" label is very clear for any experienced programmer – Sakuragaoka Nov 13 '20 at 09:28
-
no. but you can try using `continue` ? – Savannah Madison May 07 '22 at 06:40
22 Answers
Python offers you the ability to do some of the things you could do with a goto using first class functions. For example:
void somefunc(int a)
{
if (a == 1)
goto label1;
if (a == 2)
goto label2;
label1:
...
label2:
...
}
Could be done in Python like this:
def func1():
...
def func2():
...
funcmap = {1 : func1, 2 : func2}
def somefunc(a):
funcmap[a]() #Ugly! But it works.
Granted, that isn't the best way to substitute for goto. But without knowing exactly what you're trying to do with the goto, it's hard to give specific advice.
@ascobol:
Your best bet is to either enclose it in a function or use an exception. For the function:
def loopfunc():
while 1:
while 1:
if condition:
return
For the exception:
try:
while 1:
while 1:
raise BreakoutException #Not a real exception, invent your own
except BreakoutException:
pass
Using exceptions to do stuff like this may feel a bit awkward if you come from another programming language. But I would argue that if you dislike using exceptions, Python isn't the language for you. :-)

- 3,731
- 22
- 33
- 46

- 192,085
- 135
- 376
- 510
-
Use it judiciously. Exceptions in Python are faster than most other languages. But they're still slow if you go crazy with them. – Jason Baker Jan 13 '09 at 13:20
-
Just a notice: `loopfunc` will generally require inputs and some more effort to implement, but it is the best way in most cases I think. – kon psych May 20 '16 at 18:43
-
5Sorry but exceptions shouldn't be used to control program flow in this way. – ventaquil Nov 02 '20 at 08:24
I recently wrote a function decorator that enables goto
in Python, just like that:
from goto import with_goto
@with_goto
def range(start, stop):
i = start
result = []
label .begin
if i == stop:
goto .end
result.append(i)
i += 1
goto .begin
label .end
return result
I'm not sure why one would like to do something like that though. That said, I'm not too serious about it. But I'd like to point out that this kind of meta programming is actual possible in Python, at least in CPython and PyPy, and not only by misusing the debugger API as that other guy did. You have to mess with the bytecode though.

- 2,077
- 2
- 17
- 16
-
4Great decorator you made! Awesome how you can fiddle with the bytecode :-) – K.Mulier Oct 04 '18 at 17:34
-
I think, this should be the accepted answer for this question. This could be useful for many nested loops, why not? – PiMathCLanguage May 31 '19 at 10:29
-
-
I wrote a simple program to test your module. Used Python 3.9. And got an exception in your `goto.py` module at line-num `175` inside line `return _make_code(code, buf.tostring())` exception is `AttributeError: 'array.array' object has no attribute 'tostring'`. Anyway thanks for great module! Looking forward to test your module again if you manage to fix this bug. – Arty Oct 23 '21 at 09:50
-
Regarding my previous comment above - I found out that `.tostring()` was deprecated (guess since Python 3.2) and removed in Python 3.9, you have to use `.tobytes()` instead. But even with this fix I get another error in `goto.py` line `53` code `return types.CodeType(*args)` error `TypeError: an integer is required (got type bytes)`. Looks like that `args` array inside `_make_code()` has wrong number of arguments or their types, in the place where you put `bytes()` of code Python there expects `int`. – Arty Oct 23 '21 at 10:13
I found this in the official python Design and History FAQ.
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(Exception): 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.
It's very nice that this is even mentioned in the official FAQ, and that a nice solution sample is provided. I really like python because its community is treating even goto
like this ;)
-
6Abusing `goto` is a major programming foux pas to be sure, but IMO abusing exceptions to emulate `goto` is only slightly better and should still be heavily frowned upon. I would've rather the Python creators include `goto` in the language for the few occasions where it actually is useful than to disallow it because "it's bad, guys" and then recommend abusing exceptions to get the same functionality (and the same code spaghettification). – Abion47 Jul 09 '20 at 18:23
-
1This is interesting part in the FAQ. The title in the FAQ is a bit misleading since it does not actually answer the "Why" part. – Niko Föhr Dec 22 '20 at 23:09
-
@np8: I think it does in saying that .."You can use exceptions to provide a 'structured goto' ".. and that ..."Many feel that exceptions can conveniently emulate all reasonable uses of the 'go' or 'goto'". So I interpret this in "there is no dedicated goto in python because the functionality and even a bit more can "easily" be achieved using other mechanisms of the language. But I also agree that there is no real reason why it wasn't added as a "syntactical sugar". Again IMHO I think the focus is on the "structured" in contrast to a goto which could literally jump around anywhere in the code. – klaas Dec 23 '20 at 22:27
-
1Lol, I was thinking I am the first one using `try...except...` to simulate goto(because nearly all answers from internet are suggesting a 3rd part python-goto library). So trying to answer here, until see this answer. Good to see this is mentioned in official document. – bearzyj May 15 '21 at 02:29
-
What's the python equivalent to goto a previous code that was run already? – AndreGraveler Apr 04 '22 at 04:57
A working version has been made: http://entrian.com/goto/.
Note: It was offered as an April Fool's joke. (working though)
# Example 1: Breaking out from a deeply nested loop:
from goto import goto, label
for i in range(1, 10):
for j in range(1, 20):
for k in range(1, 30):
print i, j, k
if k == 3:
goto .end
label .end
print "Finished\n"
Needless to say. Yes its funny, but DONT use it.

- 1,905
- 22
- 22
-
5looks better to me than using 3 breaks... of course there are other ways to write it also. – Nick Aug 30 '17 at 19:36
-
1
To answer the @ascobol
's question using @bobince
's suggestion from the comments:
for i in range(5000):
for j in range(3000):
if should_terminate_the_loop:
break
else:
continue # no break encountered
break
The indent for the else
block is correct. The code uses obscure else
after a loop Python syntax. See Why does python use 'else' after for and while loops?

- 399,953
- 195
- 994
- 1,670
-
I fixed your else block indent, which led to an [interesting discovery](http://pastebin.com/HHMRpcQw): – Braden Best Feb 07 '15 at 16:04
-
4@B1KMusic: the indent is correct as is. It is a special Python syntax. `else` is executed *after the loop* if `break` hasn't been encountered. The effect is that `should_terminate_the_loop` terminates *both* inner and outer loops. – jfs Feb 07 '15 at 16:05
-
1I should have specified that I only made that discovery after I made the edit. Before that, I thought I had discovered a bug in the interpreter, so I made [a bunch of test cases](http://pastebin.com/HHMRpcQw) and [did some research](http://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement) to understand what was going on. Sorry about that. – Braden Best Feb 07 '15 at 16:10
-
1Now that I understand what's going on, I agree, that is some esoteric code that would be done a lot more easily with [more traditional methods](http://pastebin.com/pCnzZqfg) – Braden Best Feb 07 '15 at 16:28
-
1@B1KMusic: the method that you've suggested (copy-pasting the condition twice) is worse. Normally, `found`, `done` bolean variables could be used instead. – jfs Feb 07 '15 at 16:33
-
Well, if OP is (was) willing to resort to the risky `goto` statement, then I'd argue duplicating a small bit of code, what can't be more than around 10 characters, isn't too much of a stretch. The goal is readability, and an if..else check with a quick comment explaining it, is more readable to me than a loop full of fairly obscure syntax that's liable to make people think there's an indentation error. Personally, if I were faced with a specific need to exit out of multiple loops at once, [this](http://pastebin.com/KuiwcYBy) is about how I would do it. – Braden Best Feb 07 '15 at 16:50
-
2@B1KMusic: No. Duplicating code to workaround your ignorance is not a good solution. Yes. `return` [suggested by @Jason Baker](http://stackoverflow.com/a/438869/4279) is a good alternative to break out of deeply nested loops. – jfs Feb 07 '15 at 16:53
-
@BradenBest lol if readability was the goal they wouldn't really want the `goto` statement. usually i've found that that reduced LOC. plus just because something is considered "obscure syntax" is no reason not to use it. that's why they invented comments #forthewin – theEpsilon Jan 10 '20 at 21:07
Python 2 & 3
pip3 install goto-statement
Tested on Python 2.6 through 3.6 and PyPy.
Link: goto-statement
foo.py
from goto import with_goto
@with_goto
def bar():
label .bar_begin
...
goto .bar_begin

- 2,317
- 17
- 31
It is technically feasible to add a 'goto' like statement to python with some work. We will use the "dis" and "new" modules, both very useful for scanning and modifying python byte code.
The main idea behind the implementation is to first mark a block of code as using "goto" and "label" statements. A special "@goto" decorator will be used for the purpose of marking "goto" functions. Afterwards we scan that code for these two statements and apply the necessary modifications to the underlying byte code. This all happens at source code compile time.
import dis, new
def goto(fn):
"""
A function decorator to add the goto command for a function.
Specify labels like so:
label .foo
Goto labels like so:
goto .foo
Note: you can write a goto statement before the correspnding label statement
"""
labels = {}
gotos = {}
globalName = None
index = 0
end = len(fn.func_code.co_code)
i = 0
# scan through the byte codes to find the labels and gotos
while i < end:
op = ord(fn.func_code.co_code[i])
i += 1
name = dis.opname[op]
if op > dis.HAVE_ARGUMENT:
b1 = ord(fn.func_code.co_code[i])
b2 = ord(fn.func_code.co_code[i+1])
num = b2 * 256 + b1
if name == 'LOAD_GLOBAL':
globalName = fn.func_code.co_names[num]
index = i - 1
i += 2
continue
if name == 'LOAD_ATTR':
if globalName == 'label':
labels[fn.func_code.co_names[num]] = index
elif globalName == 'goto':
gotos[fn.func_code.co_names[num]] = index
name = None
i += 2
# no-op the labels
ilist = list(fn.func_code.co_code)
for label,index in labels.items():
ilist[index:index+7] = [chr(dis.opmap['NOP'])]*7
# change gotos to jumps
for label,index in gotos.items():
if label not in labels:
raise Exception("Missing label: %s"%label)
target = labels[label] + 7 # skip NOPs
ilist[index] = chr(dis.opmap['JUMP_ABSOLUTE'])
ilist[index + 1] = chr(target & 255)
ilist[index + 2] = chr(target >> 8)
# create new function from existing function
c = fn.func_code
newcode = new.code(c.co_argcount,
c.co_nlocals,
c.co_stacksize,
c.co_flags,
''.join(ilist),
c.co_consts,
c.co_names,
c.co_varnames,
c.co_filename,
c.co_name,
c.co_firstlineno,
c.co_lnotab)
newfn = new.function(newcode,fn.func_globals)
return newfn
if __name__ == '__main__':
@goto
def test1():
print 'Hello'
goto .the_end
print 'world'
label .the_end
print 'the end'
test1()
Hope this answers the question.

- 14,889
- 4
- 39
- 54

- 9,361
- 11
- 47
- 55
-
Great util function! (`goto()`) Works well for me. But only in Python 2. When used in Python 3 there are many exceptions (for example `new` module is Py 3 is replaced with `types`, `.func_code.` replaced with `.__code__.`, etc). Would be great if you can adopt this function to Python 3. – Arty Oct 23 '21 at 10:44
you can use User-defined Exceptions to emulate goto
example:
class goto1(Exception):
pass
class goto2(Exception):
pass
class goto3(Exception):
pass
def loop():
print 'start'
num = input()
try:
if num<=0:
raise goto1
elif num<=2:
raise goto2
elif num<=4:
raise goto3
elif num<=6:
raise goto1
else:
print 'end'
return 0
except goto1 as e:
print 'goto1'
loop()
except goto2 as e:
print 'goto2'
loop()
except goto3 as e:
print 'goto3'
loop()

- 431
- 4
- 12
Labels for break
and continue
were proposed in PEP 3136 back in 2007, but it was rejected. The Motivation section of the proposal illustrates several common (if inelegant) methods for imitating labeled break
in Python.

- 398,270
- 210
- 566
- 880
I was looking for some thing similar to
for a in xrange(1,10):
A_LOOP
for b in xrange(1,5):
for c in xrange(1,5):
for d in xrange(1,5):
# do some stuff
if(condition(e)):
goto B_LOOP;
So my approach was to use a boolean to help breaking out from the nested for loops:
for a in xrange(1,10):
get_out = False
for b in xrange(1,5):
if(get_out): break
for c in xrange(1,5):
if(get_out): break
for d in xrange(1,5):
# do some stuff
if(condition(e)):
get_out = True
break

- 1,691
- 19
- 23
Though there isn't any code equivalent to goto/label
in Python, you could still get such functionality of goto/label
using loops.
Lets take a code sample shown below where goto/label
can be used in a arbitrary language other than python.
String str1 = 'BACK'
label1:
print('Hello, this program contains goto code\n')
print('Now type BACK if you want the program to go back to the above line of code. Or press the ENTER key if you want the program to continue with further lines of code')
str1 = input()
if str1 == 'BACK'
{
GoTo label1
}
print('Program will continue\nBla bla bla...\nBla bla bla...\nBla bla bla...')
Now the same functionality of the above code sample can be achieved in python by using a while
loop as shown below.
str1 = 'BACK'
while str1 == 'BACK':
print('Hello, this is a python program containing python equivalent code for goto code\n')
print('Now type BACK if you want the program to go back to the above line of code. Or press the ENTER key if you want the program to continue with further lines of code')
str1 = input()
print('Program will continue\nBla bla bla...\nBla bla bla...\nBla bla bla...')

- 284
- 1
- 13
You can achieve it using nested methods inside python
def func1():
print("inside func1")
def inline():
print("im inside")
inline()
func1()

- 3,359
- 2
- 38
- 41
When implementing "goto", one must first ask what a goto is. While it may seem obvious, most people don't think about how goto relates to function stacks.
If you perform a "goto" inside a function, you are, in effect, abandoning the function call stack. This is considered bad practice, because function stacks are designed with the expectation that you will continue where you left off, after delegating an intermediate task. This is why gotos are used for exceptions, and exceptions can be used to emulate goto, which i will explain.
Finite state machines are probably the best use case for goto, which most of the time are implemented in a kludgy way with loops and switch statements, but I believe that "top level" gotos, are the cleanest, most semantic way to implement finite state machines. In this case, you want to make sure, if you have more variables, they are globals, and don't require encapsulation. Make sure you first model your variable state space(which may be different from execution state, ie the finite state machine).
I believe there are legitimate design reasons to use goto, exception handling being the special case where mixing goto with functions makes sense. However, in most cases, you want to restrict yourself to "top level" goto, so you never call goto within a function, but only within a global scope.
The easiest way to emulate top level goto in modern languages, is to realize that top-level gotos, simply require global variables and an empty call stack. So to keep the call stack empty, you return whenever you call a new function. Here's an example to print the first n fibonacci numbers:
a = 0
b = 1
n = 100
def A():
global a, b
a = a + b
n -= 1
print(a)
return B() if n > 0 else 0
def B():
global a, b
b = a + b
n -= 1
print(b)
return A() if n > 0 else 0
A()
While this example may be more verbose than loop implementations, it is also much more powerful and flexible, and requires no special cases. It lets you have a full finite state machine. You could also modify this with a goto runner.
def goto(target):
while(target) target = target()
def A():
global a, b
a = a + b
print(a)
return B
def B():
global a, b
b = a + b
print(b)
return A
goto(A)
To enforce the "return" part, you could write a goto function that simply throws an exception when finished.
def goto(target):
target()
throw ArgumentError("goto finished.")
def A():
global a, b
a = a + b
print(a)
goto(B)
def B()
global a, b
b = a + b
print(b)
goto(A)
goto(A)
So you see, a lot of this is overthinking, and a helper function that calls a function and then throws an error is all you need. You could further wrap it in a "start" function, so the error gets caught, but I don't think that's strictly necessary. While some of these implementations may use up your call stack, the first runner example keeps it empty, and if compilers can do tail call optimization, that helps as well.

- 161
- 1
- 7
-
1this is the nicest and clearest example for a "trampoline" in python I've seen so far --- because that's what the goto() function is: a trampoline! thank you! – Susanne Oberhauser Apr 19 '22 at 08:23
I wanted the same answer and I didnt want to use goto
. So I used the following example (from learnpythonthehardway)
def sample():
print "This room is full of gold how much do you want?"
choice = raw_input("> ")
how_much = int(choice)
if "0" in choice or "1" in choice:
check(how_much)
else:
print "Enter a number with 0 or 1"
sample()
def check(n):
if n < 150:
print "You are not greedy, you win"
exit(0)
else:
print "You are nuts!"
exit(0)

- 914
- 1
- 9
- 16
I have my own way of doing gotos. I use separate python scripts.
If I want to loop:
file1.py
print("test test")
execfile("file2.py")
a = a + 1
file2.py
print(a)
if a == 10:
execfile("file3.py")
else:
execfile("file1.py")
file3.py
print(a + " equals 10")
(NOTE: This technique only works on Python 2.x versions)
For a forward Goto, you could just add:
while True:
if some condition:
break
#... extra code
break # force code to exit. Needed at end of while loop
#... continues here
This only helps for simple scenarios though (i.e. nesting these would get you into a mess)

- 8,425
- 4
- 58
- 92
In lieu of a python goto equivalent I use the break statement in the following fashion for quick tests of my code. This assumes you have structured code base. The test variable is initialized at the start of your function and I just move the "If test: break" block to the end of the nested if-then block or loop I want to test, modifying the return variable at the end of the code to reflect the block or loop variable I'm testing.
def x:
test = True
If y:
# some code
If test:
break
return something

- 8,950
- 115
- 65
- 78

- 11
- 2
no there is an alternative way to implement goto statement
class id:
def data1(self):
name=[]
age=[]
n=1
while n>0:
print("1. for enter data")
print("2. update list")
print("3. show data")
print("choose what you want to do ?")
ch=int(input("enter your choice"))
if ch==1:
n=int(input("how many elemet you want to enter="))
for i in range(n):
name.append(input("NAME "))
age.append(int(input("age ")))
elif ch==2:
name.append(input("NAME "))
age.append(int(input("age ")))
elif ch==3:
try:
if name==None:
print("empty list")
else:
print("name \t age")
for i in range(n):
print(name[i]," \t ",age[i])
break
except:
print("list is empty")
print("do want to continue y or n")
ch1=input()
if ch1=="y":
n=n+1
else:
print("name \t age")
for i in range(n):
print(name[i]," \t ",age[i])
n=-1
p1=id()
p1.data1()

- 29,388
- 11
- 94
- 103

- 11
- 3
I think while loop is alternate for the "goto_Statement". Because after 3.6 goto loop is not working anymore. I also write an example of the while loop.
str1 = "stop"
while str1 == "back":
var1 = int(input(" Enter Ist Number: "))
var2 = int(input(" Enter 2nd Number: "))
var3 = print(""" What is your next operation
For Addition Press And Enter : 'A'
For Muliplt Press And Enter : 'M'
For Division Press And Enter : 'D'
For Subtaction Press And Enter : 'S' """)
var4 = str(input("For operation press any number : "))
if(var1 == 45) and (var2 == 3):
print("555")
elif(var1 == 56) and (var2 == 9):
print("77")
elif(var1 == 56) and (var2 == 6):
print("4")
else:
if(var4 == "A" or "a"):
print(var1 + var2)
if(var4 == "M" or "m"):
print(var1 * var2)
if(var4 == "D" or "d"):
print(var1 / var2)
if(var4 == "S" or "s"):
print(var1 - var2)
print("if you want to continue then type 'stop'")
str1 = input()
print("Strt again")

- 323
- 1
- 3
- 7
I solved this problem with functions. Only thing I did was change labels with functions. Here is a very basic code:
def goto_holiday(): #label: holiday
print("I went to holiday :)")
def goto_work(): #label: work
print("I went to work")
salary=5000
if salary>6000:
goto_holiday()
else:
goto_work()

- 477
- 4
- 15