Python with語句上下文管理器兩種實(shí)現(xiàn)方法分析
本文實(shí)例講述了Python with語句上下文管理器。分享給大家供大家參考,具體如下:
在編程中會(huì)經(jīng)常碰到這種情況:有一個(gè)特殊的語句塊,在執(zhí)行這個(gè)語句塊之前需要先執(zhí)行一些準(zhǔn)備動(dòng)作;當(dāng)語句塊執(zhí)行完成后,需要繼續(xù)執(zhí)行一些收尾動(dòng)作。例如,文件讀寫后需要關(guān)閉,數(shù)據(jù)庫讀寫完畢需要關(guān)閉連接,資源的加鎖和解鎖等情況。
對(duì)于這種情況python提供了上下文管理器(Context Manager)的概念,可以通過上下文管理器來定義/控制代碼塊執(zhí)行前的準(zhǔn)備動(dòng)作,以及執(zhí)行后的收尾動(dòng)作。
一、為何使用上下文管理器
1、不使用上下文管理器的情況
通過try...finally語句執(zhí)行異常處理和關(guān)閉句柄的動(dòng)作。
logger = open("log.txt", "w") try: logger.write('Hello ') logger.write('World') finally: logger.close() print logger.closed
2、使用上下文管理器
默認(rèn)文件Python的內(nèi)置file類型是支持上下文管理協(xié)議的。
使用上下文管理器with使得依據(jù)精簡了很多。
with open("log.txt", "w") as logger: logger.write('Hello ') logger.write('World') print logger.closed
二、實(shí)現(xiàn)上下文管理器實(shí)現(xiàn)上下文管理器有兩種方式實(shí)現(xiàn)。方法一:類實(shí)現(xiàn)__enter__和__exit__方法。方法二:contextlib模塊裝飾器和生成器實(shí)現(xiàn)。
下面我們通過兩種方法分別實(shí)現(xiàn)一個(gè)自定義的上下文管理器。
1、方法一:通過類實(shí)現(xiàn)__enter__和__exit__方法
class File(object): def __init__(self, file_name, method): self.file_obj = open(file_name, method) def __enter__(self): return self.file_obj def __exit__(self, type, value, traceback): self.file_obj.close() with File('demo.txt', 'w') as opened_file: opened_file.write('Hola!')
實(shí)現(xiàn)__enter__和__exit__方法后,就能通過with語句進(jìn)行上下文管理。
a、底層都發(fā)生了什么?
1、with語句先暫存了File類的__exit__方法,然后它調(diào)用File類的__enter__方法。
2、__enter__方法打開文件并返回給with語句,打開的文件句柄被傳遞給opened_file參數(shù)。
3、with語句調(diào)用之前暫存的__exit__方法,__exit__方法關(guān)閉了文件。
b、異常處理
關(guān)于異常處理,with語句會(huì)采取哪些步驟。
1. 它把異常的type,value和traceback傳遞給__exit__方法
2. 它讓__exit__方法來處理異常
3. 如果__exit__返回的是True,那么這個(gè)異常就被忽略。
4. 如果__exit__返回的是True以外的任何東西,那么這個(gè)異常將被with語句拋出。
異常拋出
#異常拋出,_exit__返回的是True以外的任何東西,那么這個(gè)異常將被with語句拋出 class File(object): def __init__(self, file_name, method): self.file_obj = open(file_name, method) def __enter__(self): return self.file_obj def __exit__(self, type, value, traceback): self.file_obj.close() print "type:",type print "value:",value print "traceback:",traceback with File('demo.txt', 'w') as opened_file: opened_file.undefined_function('Hola!') #output================================================ # type: <type 'exceptions.AttributeError'> # value: 'file' object has no attribute 'undefined_function' # traceback: <traceback object at 0x000000000262D9C8> # opened_file.undefined_function('Hola!') # AttributeError: 'file' object has no attribute 'undefined_function'
異常忽略:
#異常忽略,__exit__返回的是True,那么這個(gè)異常就被忽略。 class File(object): def __init__(self, file_name, method): self.file_obj = open(file_name, method) def __enter__(self): return self.file_obj def __exit__(self, exception_type, exception_value, traceback): print("Exception has been handled") self.file_obj.close() return True with File('demo.txt', 'w') as opened_file: opened_file.undefined_function('Hola!') # output================================== # Exception has been handled
2、方法二:contextlib模塊裝飾器和生成器實(shí)現(xiàn)
這種方式實(shí)現(xiàn)更優(yōu)雅,我個(gè)人更喜歡這種方式。
yield之前的代碼由__enter__
方法執(zhí)行,yield之后的代碼由__exit__
方法執(zhí)行。本質(zhì)上還是__enter__
和__exit__
方法。
# coding:utf-8 import contextlib @contextlib.contextmanager def myopen(filename, mode): f = open(filename, mode) try: yield f.readlines() except Exception as e: print e finally: f.close() if __name__ == '__main__': with myopen(r'c:\ip2.txt', 'r') as f: for line in f: print line
3、with語句上多個(gè)下文關(guān)聯(lián)
直接通過一個(gè)with語句打開多個(gè)上下文,即可同時(shí)使用多個(gè)上下文變量,而不必需嵌套使用with語句。
class File(object): def __init__(self, file_name, method): self.file_obj = open(file_name, method) def __enter__(self): return self.file_obj def __exit__(self, exception_type, exception_value, traceback): self.file_obj.close() return True with File('demo.txt', 'w') as f1,File('demo.txt','w') as f2: print f1,f2 # Output============================# <open file 'demo.txt', mode 'w' at 0x000000000263D150> <open file 'demo.txt', mode 'w' at 0x000000000263D1E0>
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Python中利用all()來優(yōu)化減少判斷的實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于Python中利用all()來優(yōu)化減少判斷的實(shí)例分析內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-06-06python批處理將圖片進(jìn)行放大實(shí)例代碼
最近處理一些規(guī)格不一的照片,需要修改成指定尺寸便于打印,下面這篇文章主要給大家介紹了關(guān)于python批處理將圖片進(jìn)行放大的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12Pytorch 實(shí)現(xiàn)變量類型轉(zhuǎn)換
這篇文章主要介紹了Pytorch 實(shí)現(xiàn)變量類型轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05