新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:final作用域的代碼一定會被執(zhí)行嗎?
正常的情況下,finally作用域的代碼一定會被執(zhí)行的,不管是否發(fā)生異常。哪怕是調用了sys.exit函數(shù),finally也是會被執(zhí)行的。

那怎么樣才能讓finally代碼不執(zhí)行了。
import time choice = True try: if choice: while True: pass else: print "Please pull the plug on your computer sometime soon..." time.sleep(60 * 60 * 24 * 365) finally: print "Finally ..."
上面的代碼主要是通過讓流程停滯在try作用域里,從而實現(xiàn)了需求。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:final作用域的代碼一定會被執(zhí)行嗎?
網(wǎng)頁路徑:http://fisionsoft.com.cn/article/djppieh.html


咨詢
建站咨詢
