本人學(xué)習(xí)地址:https://blog.csdn.net/bitcarmanlee/article/details/52745676,
1.with語(yǔ)法
with 上下文表達(dá)式 as 對(duì)象名
語(yǔ)句體
2.名詞解釋
- 上下文管理協(xié)議(Context Management Protocol): 包含
__enter()__
與__exit()__
方法的即滿足上下文管理協(xié)議。 - 上下文管理器(Context Manager):實(shí)現(xiàn)
__enter()__
與__exit()__
方法的就為上下文管理器。 - 運(yùn)行時(shí)上下文(Runtime Context):由上下文管理器創(chuàng)建,
__enter__()
方法在語(yǔ)句體執(zhí)行之前進(jìn)入運(yùn)行時(shí)上下文,__exit__()
在語(yǔ)句體執(zhí)行完后從運(yùn)行時(shí)上下文退出。 - 上下文表達(dá)式(Context Expression):with 語(yǔ)句中跟在關(guān)鍵字 with 之后的表達(dá)式,該表達(dá)式要返回一個(gè)上下文管理器對(duì)象。
- 語(yǔ)句體(With-Body):with 語(yǔ)句包裹起來(lái)的代碼塊,在執(zhí)行語(yǔ)句體之前會(huì)調(diào)用上下文管理器的
__enter__()
方法,執(zhí)行完語(yǔ)句體之后會(huì)執(zhí)行__exit__()
方法。
3.小栗子
(1).file
常見(jiàn)的文件操作中,我們經(jīng)常忘記關(guān)閉文件句柄,或者會(huì)寫(xiě)一串try...except去捕獲異常,可以是用with去簡(jiǎn)化代碼:
with open("/temp.txt") as file:
data = file.read()
image.png
open
方法返回一個(gè)file,file 是不是我們要找的上下文管理器呢?image.png
file 類(lèi)里實(shí)現(xiàn)了
__enter()__,__exit()__
方法,所以為上下文管理器,所以file可以使用with語(yǔ)句。
(2)自定義上下文管理器
class sample():
def __enter__(self):
print('enter')
def __exit__(self, exc_type, exc_val, exc_tb):
print ('exit')
with sample() as sample:
print('with body')
運(yùn)行結(jié)果
image.png
從中可以看出,執(zhí)行順序?yàn)椋?enter方法 ->withbody->exit方法
class CustomException(Exception):
def __init__(self):
print('custom exception')
Exception.__init__(self, 'custom exception')
class sample():
def __enter__(self):
print('enter')
def __exit__(self, exc_type, exc_val, exc_tb):
if isinstance(exc_val,CustomException):
print ('exit custom exception')
print ('exit')
with sample() as sample:
print('with body 1')
raise CustomException()
print('with body 2')
image.png
這次執(zhí)行順序?yàn)?enter->with body 1->custom exception->exit custom exception->exit
說(shuō)明當(dāng)拋出異常時(shí),也是執(zhí)行exit方法。