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”。

相关文章:

  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2022-12-23
  • 2021-09-01
  • 2022-03-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-08-01
相关资源
相似解决方案