python with

術(shù)語

要使用 with 語句,首先要明白上下文管理器這一概念。有了上下文管理器,with 語句才能工作。
下面是一組與上下文管理器和with 語句有關(guān)的概念。
上下文管理協(xié)議(Context Management Protocol):包含方法 __enter__()__exit__() ,支持
該協(xié)議的對(duì)象要實(shí)現(xiàn)這兩個(gè)方法。
上下文管理器(Context Manager):支持上下文管理協(xié)議的對(duì)象,這種對(duì)象實(shí)現(xiàn)了
__enter__()__exit__() 方法。上下文管理器定義執(zhí)行 with 語句時(shí)要建立的運(yùn)行時(shí)上下文,
負(fù)責(zé)執(zhí)行 with 語句塊上下文中的進(jìn)入與退出操作。通常使用 with 語句調(diào)用上下文管理器,
也可以通過直接調(diào)用其方法來使用。
運(yùn)行時(shí)上下文(runtime context):由上下文管理器創(chuàng)建,通過上下文管理器的 __enter__()
__exit__() 方法實(shí)現(xiàn),__enter__() 方法在語句體執(zhí)行之前進(jìn)入運(yùn)行時(shí)上下文,__exit__()
語句體執(zhí)行完后從運(yùn)行時(shí)上下文退出。with 語句支持運(yùn)行時(shí)上下文這一概念。
上下文表達(dá)式(Context Expression):with 語句中跟在關(guān)鍵字 with 之后的表達(dá)式,該表達(dá)式
要返回一個(gè)上下文管理器對(duì)象。
語句體(with-body):with 語句包裹起來的代碼塊,在執(zhí)行語句體之前會(huì)調(diào)用上下文管理器的__enter__() 方法,執(zhí)行完語句體之后會(huì)執(zhí)行__exit__() 方法。


1.基本語法和工作原理
with context_expression [as target(s)]:
        with-body
with open(r'somefileName') as somefile:
        for line in somefile:
            print line
            # ...more code

等價(jià)于傳統(tǒng)方式, try/finally 方式操作文件對(duì)象

 somefile = open(r'somefileName')
    try:
        for line in somefile:
            print line
            # ...more code
    finally:
        somefile.close()

with 語句的執(zhí)行過程類似如下代碼塊:

    context_manager = context_expression
    exit = type(context_manager).__exit__  
    value = type(context_manager).__enter__(context_manager)
    exc = True   # True 表示正常執(zhí)行,即便有異常也忽略;False 表示重新拋出異常,需要對(duì)異常進(jìn)行處理
    try:
        try:
            target = value  # 如果使用了 as 子句
            with-body     # 執(zhí)行 with-body
        except:
            # 執(zhí)行過程中有異常發(fā)生
            exc = False
            # 如果 __exit__ 返回 True,則異常被忽略;如果返回 False,則重新拋出異常
            # 由外層代碼對(duì)異常進(jìn)行處理
            if not exit(context_manager, *sys.exc_info()):
                raise
    finally:
        # 正常退出,或者通過 statement-body 中的 break/continue/return 語句退出
        # 或者忽略異常退出
        if exc:
            exit(context_manager, None, None, None) 
        # 缺省返回 None,None 在布爾上下文中看做是 False

1.執(zhí)行 context_expression,生成上下文管理器 context_manager
2.調(diào)用上下文管理器的__enter__() 方法;如果使用了 as 子句,則將__enter__() 方法的返回值賦值給 as 子句中的 target(s)
3.執(zhí)行語句體 with-body
4.不管是否執(zhí)行過程中是否發(fā)生了異常,執(zhí)行上下文管理器的 __exit__() 方法,__exit__() 方法負(fù)責(zé)執(zhí)行“清理”工作,如釋放資源等。如果執(zhí)行過程中沒有出現(xiàn)異常,或者語句體中執(zhí)行了語句 break/continue/return,則以 None 作為參數(shù)調(diào)用__exit__(None, None, None) ;如果執(zhí)行過程中出現(xiàn)異常,則使用 sys.exc_info 得到的異常信息為參數(shù)調(diào)用__exit__(exc_type, exc_value, exc_traceback)
5.出現(xiàn)異常時(shí),如果 __exit__(type, value, traceback) 返回 False,則會(huì)重新拋出異常,讓with 之外的語句邏輯來處理異常,這也是通用做法;如果返回 True,則忽略異常,不再對(duì)異常進(jìn)行處理


2.自定義上下文管理器

自定義的上下文管理器要實(shí)現(xiàn)上下文管理協(xié)議所需要的 __enter__()__exit__() 兩個(gè)方法
(1).context_manager.__enter__():進(jìn)入上下文管理器的運(yùn)行時(shí)上下文,在語句體執(zhí)行前調(diào)用。with 語句將該方法的返回值賦值給 as 子句中的 target,如果指定了 as 子句的話
(2).context_manager.__exit__(exc_type, exc_value, exc_traceback) :退出與上下文管理器相關(guān)的運(yùn)行時(shí)上下文,返回一個(gè)布爾值表示是否對(duì)發(fā)生的異常進(jìn)行處理。參數(shù)表示引起退出操作的異常,如果退出時(shí)沒有發(fā)生異常,則3個(gè)參數(shù)都為None。如果發(fā)生異常,返回True 表示不處理異常,否則會(huì)在退出該方法后重新拋出異常以由 with 語句之外的代碼邏輯進(jìn)行處理。如果該方法內(nèi)部產(chǎn)生異常,則會(huì)取代由 statement-body 中語句產(chǎn)生的異常。要處理異常時(shí),不要顯示重新拋出異常,即不能重新拋出通過參數(shù)傳遞進(jìn)來的異常,只需要將返回值設(shè)置為 False 就可以了。之后,上下文管理代碼會(huì)檢測(cè)是否__exit__()失敗來處理異常

假設(shè)有一個(gè)資源 DummyResource,這種資源需要在訪問前先分配,使用完后再釋放掉;分配操作可以放到 enter() 方法中,釋放操作可以放到 exit() 方法中。簡單起見,這里只通過打印語句來表明當(dāng)前的操作,并沒有實(shí)際的資源分配與釋放。
自定義支持 with 語句的對(duì)象

 class DummyResource:
    def __init__(self, tag):
            self.tag = tag
            print 'Resource [%s]' % tag
        def __enter__(self):
            print '[Enter %s]: Allocate resource.' % self.tag
            return self   # 可以返回不同的對(duì)象
        def __exit__(self, exc_type, exc_value, exc_tb):
            print '[Exit %s]: Free resource.' % self.tag
            if exc_tb is None:
                print '[Exit %s]: Exited without exception.' % self.tag
            else:
                print '[Exit %s]: Exited with exception raised.' % self.tag
                return False   # 可以省略,缺省的None也是被看做是False

使用自定義的支持 with 語句的對(duì)象

  with DummyResource('Normal'):
        print '[with-body] Run without exceptions.'

    with DummyResource('With-Exception'):
        print '[with-body] Run with exception.'
        raise Exception
        print '[with-body] Run with exception. Failed to finish statement-body!'

執(zhí)行結(jié)果

 Resource [Normal]
    [Enter Normal]: Allocate resource.
    [with-body] Run without exceptions.
    [Exit Normal]: Free resource.
    [Exit Normal]: Exited without exception.

Resource [With-Exception]
    [Enter With-Exception]: Allocate resource.
    [with-body] Run with exception.
    [Exit With-Exception]: Free resource.
    [Exit With-Exception]: Exited with exception raised.

    Traceback (most recent call last):
      File "G:/demo", line 20, in <module>
       raise Exception
    Exception

3.contextlib 模塊

contextlib 模塊提供了3個(gè)對(duì)象:裝飾器 contextmanager、函數(shù) nested 和上下文管理器 closing。使用這些對(duì)象,可以對(duì)已有的生成器函數(shù)或者對(duì)象進(jìn)行包裝,加入對(duì)上下文管理協(xié)議的支持,避免了專門編寫上下文管理器來支持 with 語句。
裝飾器 contextmanager
contextmanager 用于對(duì)生成器函數(shù)進(jìn)行裝飾,生成器函數(shù)被裝飾以后,返回的是一個(gè)上下文管理器,其 __enter__()__exit__()方法由 contextmanager 負(fù)責(zé)提供,而不再是之前的迭代子。被裝飾的生成器函數(shù)只能產(chǎn)生一個(gè)值,否則會(huì)導(dǎo)致異常 RuntimeError;產(chǎn)生的值會(huì)賦值給 as 子句中的 target,如果使用了 as 子句的話。下面看一個(gè)簡單的例子。
裝飾器 contextmanager 使用示例

 from contextlib import contextmanager

    @contextmanager
    def demo():
        print '[Allocate resources]'
        print 'Code before yield-statement executes in __enter__'
        yield '*** contextmanager demo ***'
        print 'Code after yield-statement executes in __exit__'
        print '[Free resources]'

    with demo() as value:
        print 'Assigned Value: %s' % value

** contextmanager 使用示例執(zhí)行結(jié)果**

 [Allocate resources]
    Code before yield-statement executes in __enter__
    Assigned Value: *** contextmanager demo ***
    Code after yield-statement executes in __exit__
    [Free resources]

可以看到,生成器函數(shù)中 yield 之前的語句在__enter__()方法中執(zhí)行,yield 之后的語句在__exit__()中執(zhí)行,而 yield 產(chǎn)生的值賦給了 as 子句中的 value 變量。
需要注意的是,contextmanager 只是省略了__enter__() / __exit__()的編寫,但并不負(fù)責(zé)實(shí)現(xiàn)資源的“獲取”和“清理”工作;“獲取”操作需要定義在 yield 語句之前,“清理”操作需要定義 yield 語句之后,這樣 with 語句在執(zhí)行 __enter__() / __exit__()方法時(shí)會(huì)執(zhí)行這些語句以獲取/釋放資源,即生成器函數(shù)中需要實(shí)現(xiàn)必要的邏輯控制,包括資源訪問出現(xiàn)錯(cuò)誤時(shí)拋出適當(dāng)?shù)漠惓!?br> 函數(shù) nested
nested 可以將多個(gè)上下文管理器組織在一起,避免使用嵌套 with 語句。

 with nested(A(), B(), C()) as (X, Y, Z):
         # with-body code here

nested 執(zhí)行過程

 with A() as X:
        with B() as Y:
            with C() as Z:
                 # with-body code here

需要注意的是,發(fā)生異常后,如果某個(gè)上下文管理器的 exit() 方法對(duì)異常處理返回 False,則更外層的上下文管理器不會(huì)監(jiān)測(cè)到異常。
上下文管理器 closing
上下文管理 closing 實(shí)現(xiàn)

  class closing(object):
        # help doc here
        def __init__(self, thing):
            self.thing = thing
        def __enter__(self):
            return self.thing
        def __exit__(self, *exc_info):
            self.thing.close()

上下文管理器會(huì)將包裝的對(duì)象賦值給 as 子句的 target 變量,同時(shí)保證打開的對(duì)象在 with-body 執(zhí)行完后會(huì)關(guān)閉掉。closing 上下文管理器包裝起來的對(duì)象必須提供 close() 方法的定義,否則執(zhí)行時(shí)會(huì)報(bào) AttributeError 錯(cuò)誤。
自定義支持 closing 的對(duì)象

class ClosingDemo(object):
        def __init__(self):
            self.acquire()
        def acquire(self):
            print 'Acquire resources.'
        def free(self):
            print 'Clean up any resources acquired.'
        def close(self):
            self.free()

    with closing(ClosingDemo()):
        print 'Using resources'

** 自定義 closing 對(duì)象的輸出結(jié)果**

Acquire resources.
    Using resources
    Clean up any resources acquired.

closing 適用于提供了 close() 實(shí)現(xiàn)的對(duì)象,比如網(wǎng)絡(luò)連接、數(shù)據(jù)庫連接等,也可以在自定義類時(shí)通過接口 close() 來執(zhí)行所需要的資源“清理”工作。
[1]https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/#icomments

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

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