0%

在Python中使用goto语句

安装一个模块:goto-statement

pip3 install goto-statement`
  1. 导入goto模块

  2. 使用@with_goto修饰为函数整个goto能力

  3. label .xxx,增加标签,标签需要以“.”开头

  4. goto .xxx,跳转到指定的标签处

示例代码:

from goto import with_goto

@with_goto
def Test():
    label .start
    print('start...')
    label .step1
    print('step1')
    label .step2
    print('step2')
    goto .start

方法来自:https://steemit.com/cn/@brysj22952/python-goto-learn-for-python-goto-statement