相当于python中的GOTO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18863309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The equivalent of a GOTO in python
提问by Calder Hutchins
I am self teaching myself python 2.7. I have some experience in using BATCH, which has a GOTO statement. How do I do that in python? For example, suppose I want to jump from line 5 to line 18.
我正在自学python 2.7。我有一些使用 BATCH 的经验,它有一个 GOTO 语句。我如何在 python 中做到这一点?例如,假设我想从第 5 行跳到第 18 行。
I realize there have been previous questions regarding this topic, but I have not found them sufficiently informative or, are too high level in python for my current understanding.
我意识到以前有关于这个主题的问题,但我没有发现它们的信息量足够大,或者对于我目前的理解来说,python 的水平太高了。
采纳答案by óscar López
Goto
s are universally reviled in computer science and programming as they lead to very unstructured code.
Goto
s 在计算机科学和编程中普遍受到谴责,因为它们会导致非常非结构化的代码。
Python (like almost every programming language today) supports structured programmingwhich controls flow using if/then/else, loop and subroutines.
Python(就像今天几乎所有的编程语言一样)支持结构化编程,它使用 if/then/else、循环和子例程来控制流程。
The key to thinking in a structured way is to understand how and why you are branching on code.
以结构化方式思考的关键是了解如何以及为何在代码上进行分支。
For example, lets pretend Python had a goto
and corresponding label
statement shudder. Look at the following code. In it if a number is greater than or equal to 0 we print if it
例如,让我们假设 Python 有一个goto
和相应的label
语句shudder。看下面的代码。在其中如果一个数字大于或等于 0 我们打印它
number = input()
if number < 0: goto negative
if number % 2 == 0:
print "even"
else:
print "odd"
goto end
label: negative
print "negative"
label: end
print "all done"
If we want to know when a piece of code is executed, we need to carefully traceback in the program, and examine how a label was arrived at - which is something that can't really be done.
如果我们想知道一段代码是什么时候执行的,我们需要在程序中仔细回溯,并检查一个标签是如何到达的——这是无法真正做到的。
For example, we can rewrite the above as:
例如,我们可以将上面的改写为:
number = input()
goto check
label: negative
print "negative"
goto end
label: check
if number < 0: goto negative
if number % 2 == 0:
print "even"
else:
print "odd"
goto end
label: end
print "all done"
Here, there are two possible ways to arrive at the "end", and we can't know which one was chosen. As programs get large this kind of problem gets worse and results in spaghetti code
在这里,到达“终点”有两种可能的方式,我们不知道选择了哪一种。随着程序变大,这种问题会变得更糟并导致意大利面条式代码
In comparison, below is how you wouldwrite this program in Python:
相比之下,下面是你将如何用 Python 编写这个程序:
number = input()
if number >= 0:
if number % 2 == 0:
print "even"
else:
print "odd"
else:
print "negative"
print "all done"
I can look at a particular line of code, and know under what conditions it is met by tracing back the tree of if/then/else
blocks it is in. For example, I know that the line print "odd"
will be run when a ((number >= 0) == True) and ((number % 2 == 0) == False)
.
我可以查看特定的代码行,并通过追溯if/then/else
它所在的块树来了解它在什么条件下满足。例如,我知道print "odd"
当((number >= 0) == True) and ((number % 2 == 0) == False)
.
回答by óscar López
There's no goto
instruction in the Python programming language. You'll have to write your code in a structuredway... But really, why do you want to use a goto
? that's been considered harmfulfor decades, and any program you can think of can be written without using goto
.
goto
Python 编程语言中没有说明。您必须以结构化的方式编写代码……但实际上,您为什么要使用goto
?几十年来,这一直被认为是有害的,您可以想到的任何程序都可以在不使用goto
.
Of course, there are some caseswhere an unconditional jump might be useful, but it's never mandatory, there will always exist a semantically equivalent, structured solution that doesn't need goto
.
当然,在某些情况下,无条件跳转可能有用,但它从来不是强制性的,总会存在不需要的语义等效的结构化解决方案goto
。
回答by Caleb Hattingh
Disclaimer: I have been exposed to a significant amount of F77
免责声明:我接触了大量的 F77
The modern equivalent of goto
(arguable, only my opinion, etc) is explicit exception handling:
goto
(有争议的,只有我的意见等)的现代等价物是显式异常处理:
Edited to highlight the code reuse better.
编辑以更好地突出代码重用。
Pretend pseudocode in a fake python-like language with goto
:
用类似 python 的伪语言假装伪代码goto
:
def myfunc1(x)
if x == 0:
goto LABEL1
return 1/x
def myfunc2(z)
if z == 0:
goto LABEL1
return 1/z
myfunc1(0)
myfunc2(0)
:LABEL1
print 'Cannot divide by zero'.
Compared to python:
与蟒蛇相比:
def myfunc1(x):
return 1/x
def myfunc2(y):
return 1/y
try:
myfunc1(0)
myfunc2(0)
except ZeroDivisionError:
print 'Cannot divide by zero'
Explicit named exceptions are a significantlybetter way to deal with non-linear conditional branching.
显式命名异常是处理非线性条件分支的明显更好的方法。
回答by Tim Peters
Forgive me - I couldn't resist ;-)
原谅我 - 我无法抗拒;-)
def goto(linenum):
global line
line = linenum
line = 1
while True:
if line == 1:
response = raw_input("yes or no? ")
if response == "yes":
goto(2)
elif response == "no":
goto(3)
else:
goto(100)
elif line == 2:
print "Thank you for the yes!"
goto(20)
elif line == 3:
print "Thank you for the no!"
goto(20)
elif line == 20:
break
elif line == 100:
print "You're annoying me - answer the question!"
goto(1)
回答by scohe001
I entirely agree that goto
is poor poor coding, but no one has actually answered the question. There isin fact a goto module for Python(though it was released as an April fool joke and is not recommended to be used, it doeswork).
我完全同意这goto
是糟糕的编码,但没有人真正回答过这个问题。还有就是其实是一个为Python转到模块(尽管它被发布了作为一个愚人节的玩笑,不建议使用,但不工作)。
回答by Paul Becotte
answer = None
while True:
answer = raw_input("Do you like pie?")
if answer in ("yes", "no"): break
print "That is not a yes or a no"
Would give you what you want with no goto statement.
不会给你你想要的东西,没有 goto 语句。