Python with語句

本人學(xué)習(xí)地址:https://blog.csdn.net/bitcarmanlee/article/details/52745676,

1.with語法

with 上下文表達(dá)式 as 對象名
  語句體

2.名詞解釋

  • 上下文管理協(xié)議(Context Management Protocol): 包含__enter()____exit()__方法的即滿足上下文管理協(xié)議。
  • 上下文管理器(Context Manager):實現(xiàn)__enter()____exit()__方法的就為上下文管理器。
  • 運行時上下文(Runtime Context):由上下文管理器創(chuàng)建,__enter__() 方法在語句體執(zhí)行之前進(jìn)入運行時上下文,__exit__() 在語句體執(zhí)行完后從運行時上下文退出。
  • 上下文表達(dá)式(Context Expression):with 語句中跟在關(guān)鍵字 with 之后的表達(dá)式,該表達(dá)式要返回一個上下文管理器對象。
  • 語句體(With-Body):with 語句包裹起來的代碼塊,在執(zhí)行語句體之前會調(diào)用上下文管理器的 __enter__() 方法,執(zhí)行完語句體之后會執(zhí)行__exit__() 方法。

3.小栗子

(1).file

常見的文件操作中,我們經(jīng)常忘記關(guān)閉文件句柄,或者會寫一串try...except去捕獲異常,可以是用with去簡化代碼:

with open("/temp.txt") as file:
    data = file.read()

image.png

open方法返回一個file,file 是不是我們要找的上下文管理器呢?
image.png

file 類里實現(xiàn)了__enter()__,__exit()__方法,所以為上下文管理器,所以file可以使用with語句。

(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')

運行結(jié)果


image.png

從中可以看出,執(zhí)行順序為: 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í)行順序為 enter->with body 1->custom exception->exit custom exception->exit
說明當(dāng)拋出異常時,也是執(zhí)行exit方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容