在条件上等同于GOTO,Python

Mak*_*aks 5 python goto conditional-statements

由于Python中没有goto运算符,可以使用哪种技术呢?

条件如果是,则转到线程1,如果为false,则转到线程2在线程中我们做一些小事情,之后我们转到线程2,其中所有其他操作都发生.

det*_*tly 14

由于Python中没有goto运算符,可以使用哪种技术呢?

逻辑上和语义上构建代码.

if condition:
    perform_some_action()

perform_other_actions()
Run Code Online (Sandbox Code Playgroud)


Opt*_*nic 6

def thread_1():
  # Do thread_1 type stuff here.

def thread_2():
  # Do thread_2 type stuff here.

if condition:
    thread_1()

# If condition was false, just run thread_2(). 
# If it was true then thread_1() will return to this point.
thread_2()
Run Code Online (Sandbox Code Playgroud)

编辑:我假设"线程"是指一段代码(也称为子程序或函数).如果您在并行执行中讨论线程,那么您将需要更多详细信息.


st0*_*0le 5

据我所知,它不存在(谢天谢地),但你应该检查这个链接

"goto"模块是2004年4月1日发布的愚人节的笑话.是的,它有效,但它仍然是一个笑话.请不要在实际代码中使用它!

  • 不要诱惑他进入Python编程的黑暗面:-)幸运的是,它写在页面上,这只是一个愚人节的笑话. (3认同)