[英]Why is a `return` statement inside of except block silently not executed if finally block contains a return statement?
[英]Why "finally:" block is executed after the return statement in a "try:" block in python?
按照我的理解:return就是返回一個值的意思。
一個例子,python1 腳本:
def func():
try:
print 98
return 'ok'
finally:
print 98
print fun()
腳本的輸出是:
98
98
好的
所以我的問題是為什么腳本的輸出不是:
98
好的
98
為什么最后是OK行的輸出?
因為當你使用
try:
#some code
finally:
#some other code
無論在try
塊中發生什么, finally
塊都保證在try
塊之后執行。 即使沒有引發異常。
finally
一般用於釋放資源、清理變量等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.