Design and History FAQ for Python3

Source : Design and History FAQ for Python3


Why is there no goto?

你可以通过异常来获得一个可以跨函数调用的 “goto 结构”。通过异常可以模拟出C、Fortran 以及其他语言中的 “go” 或 “goto” 的用法。

class label(Exception): pass  # 声明个标签

try:
    ...
    if condition: raise label()  # goto 跳转到标签位置
    ...
except label:  # goto 要跳转的目的地
    pass
...

虽然这样并无法让你跳进循环,但那种做法通常被认为是在滥用 “goto”。

posted @ 2019-01-06 18:32  暮晨  阅读(107)  评论(0编辑  收藏  举报

Aaron Swartz was and will always be a hero