最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】Python中的goto

Python crifan 10990浏览 0评论

python中没有goto,好像也有其他模块模拟出来goto供有需要的人使用,但是很明显,goto破坏程序的逻辑,所以一般来说,也不推荐使用goto,能不用,最好不要用。

但是,很多时候,我们又有类似需求。

经过测试,自己写了个,类似goto的实现,供参考:

# following is emulation of goto
MaxAllowRetryNumber = 3
for tries in range(MaxAllowRetryNumber + 1) :
    try :
        print tries
        # do what you want normally do here
        break # successfully excute, so break now
    except :
        logging.info("do something fail, do %d retry", tries)
        continue # do retry

转载请注明:在路上 » 【整理】Python中的goto

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.167 seconds, using 22.10MB memory