Is there a label/goto in Python?

2019-01-01 06:45发布

Is there a goto or any equivalent in Python to be able to jump to a specific line of code?

标签: python goto
15条回答
ら面具成の殇う
2楼-- · 2019-01-01 07:24

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()
查看更多
像晚风撩人
3楼-- · 2019-01-01 07:25

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)
查看更多
有味是清欢
4楼-- · 2019-01-01 07:25

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)

查看更多
泪湿衣
5楼-- · 2019-01-01 07:29

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.

查看更多
忆尘夕之涩
6楼-- · 2019-01-01 07:32

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)

查看更多
千与千寻千般痛.
7楼-- · 2019-01-01 07:33

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.

查看更多
登录 后发表回答