国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python上下文管理器實(shí)現(xiàn)方法總結(jié)

瀏覽:72日期:2022-06-15 08:00:11
目錄什么時(shí)候可以考慮上下文管理器方法1(上下文管理器協(xié)議)方法2(@contextmanager)方法3(contextlib.closing())什么時(shí)候可以考慮上下文管理器

當(dāng)你的代碼邏輯需要用到如下關(guān)鍵字時(shí),可以考慮使用上下文管理器讓你的代碼更加優(yōu)雅:

try:...finally:...

接下來(lái)介紹實(shí)現(xiàn)上下文管理器的三種方法。

方法1(上下文管理器協(xié)議)

總所周知,open()是默認(rèn)支持上下文管理器的。所以打開(kāi)一個(gè)txt文件,并向里面寫(xiě)入內(nèi)容,再關(guān)閉這個(gè)文件的代碼可以這樣寫(xiě):

with open('1.txt', 'w') as file:file.write('this is a demo')

這是等同于:

file = Nonetry: file = open('1.txt', 'w') file.write('this is a demo')finally: file.close()

要在Python中實(shí)現(xiàn)with語(yǔ)句的使用,就需要借助上下文管理器協(xié)議。也就是需要實(shí)現(xiàn)__enter__和__exit__兩個(gè)魔法方法。

class OpenMyFile(object): def __init__(self, path):self.path = path def __enter__(self):print('opening the txt')self.f = open(self.path, 'w')return self def __exit__(self, *args, **kwargs):print('closing the txt')self.f.close() def write(self, string):print('writing...')self.f.write(string)with OpenMyFile('2.txt') as file: file.write('this is a demo2')# 輸出:opening the txtwriting...closing the txt

同時(shí)能夠看到本地生成了2.txt文件。需要注意的是,__enter__得return實(shí)例對(duì)象,不然會(huì)報(bào)異常:AttributeError: ’NoneType’ object has no attribute ’write’

這是因?yàn)镻ython中的函數(shù)默認(rèn)返回None。

方法2(@contextmanager)

利用contextlib中的contextmanager裝飾器。

from contextlib import contextmanager@contextmanagerdef open_my_file(path): print('opening the txt') f = open('3.txt', 'w') yield f print('closing the txt') f.close()with open_my_file('3.txt') as file: file.write('this is demo3')# 輸出:opening the txtclosing the txt

在@contextmanager裝飾的函數(shù)中,需要用yield隔開(kāi)兩個(gè)邏輯語(yǔ)句。這里yield出來(lái)的對(duì)象會(huì)被as后面的變量接收。

方法3(contextlib.closing())

利用contextlib中的closing()方法。

from contextlib import closingclass OpenMyFile(object): def __init__(self, path):print('opening the txt')self.f = open(path, 'w') def write(self, string):self.f.write(string) def close(self):print('closing the txt')self.f.close()with closing(OpenMyFile('4.txt')) as file: file.write('this is demo4')# 輸出:opening the txtclosing the txt

與方法1不同。經(jīng)過(guò)closing()方法包裝過(guò)后,在with語(yǔ)句結(jié)束時(shí),會(huì)強(qiáng)制調(diào)用對(duì)象的close()方法。所以使用方法3時(shí),需要定義的方法不是__exit__()而是close()。

到此這篇關(guān)于Python上下文管理器實(shí)現(xiàn)方法總結(jié)的文章就介紹到這了,更多相關(guān)Python上下文管理器實(shí)現(xiàn)的三種方法內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

主站蜘蛛池模板: 美女黄色在线看 | 久久久亚洲精品蜜桃臀 | 亚洲爱爱爱 | 久草免费精品视频 | 欧美一级片 在线播放 | 亚洲一区二区中文 | 日韩欧美综合 | 久久黄色影片 | 日本成人午夜 | 成 人 a v黄 色| 欧美一级毛片免费看高清 | 美日韩一区二区三区 | 一道精品视频一区二区三区图片 | 久香草视频在线观看 | 毛片1级 | 国产成人精视频在线观看免费 | 男人精品一线视频在线观看 | 韩国在线精品福利视频在线观看 | 永久毛片| 深夜福利视频在线观看免费播放 | 国内一级特黄女人精品片 | 久久精品国产三级不卡 | 久久影院yy6080| 99re免费99re在线视频手机版 | 在线视频 一区二区 | 成人精品第一区二区三区 | 欧美成人久久一级c片免费 欧美成人看片黄a免费 | 中国一级性生活片 | 成年人免费视频网站 | 99国产精品欧美久久久久久影院 | 国产在线一区二区三区在线 | 久久99精品久久只有精品 | 99精品久久99久久久久久 | 三级国产精品 | 美国亚洲成年毛片 | 99精品热女视频专线 | 亚洲成a人一区二区三区 | 看一级特黄a大片日本片 | 亚洲羞羞裸色私人影院 | 国内精品九一在线播放 | 久草久草|