亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁/技術文章
文章詳情頁

Python上下文管理器實現方法總結

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

當你的代碼邏輯需要用到如下關鍵字時,可以考慮使用上下文管理器讓你的代碼更加優雅:

try:...finally:...

接下來介紹實現上下文管理器的三種方法。

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

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

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中實現with語句的使用,就需要借助上下文管理器協議。也就是需要實現__enter__和__exit__兩個魔法方法。

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

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

這是因為Python中的函數默認返回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裝飾的函數中,需要用yield隔開兩個邏輯語句。這里yield出來的對象會被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不同。經過closing()方法包裝過后,在with語句結束時,會強制調用對象的close()方法。所以使用方法3時,需要定義的方法不是__exit__()而是close()。

到此這篇關于Python上下文管理器實現方法總結的文章就介紹到這了,更多相關Python上下文管理器實現的三種方法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

主站蜘蛛池模板: 99热精品国产三级在线观看 | 亚洲午夜久久影院 | 男生福利网站 | 欧美一级片毛片 | 欧美黑人与白人做爰 | 2022色婷婷综合久久久 | 亚洲一区二区三区视频 | 一区二区三区毛片免费 | 污污网站免费 | 免费高清视频日本 | 毛片在线播放a | 一级成人a做片免费 | 久久精品国产一区二区三区日韩 | 国产乱熟肥女视频网站 | 97精品国产| 国产精美视频 | 国产真实强j视频在线观看 国产真实露脸4p视频 | 国产成人一区二区三区精品久久 | 精品一区二区三区影院在线午夜 | 99国产精品高清一区二区二区 | 免费a级毛片无码 | 国产a级三级三级三级中国 国产a级午夜毛片 | 高清成人综合 | 日日噜噜夜夜狠狠视频无 | 久久91久久91精品免费观看 | 91精品国产露脸在线观看 | 在线观看免费国产视频 | 欧美精品在线一区二区三区 | 国产欧美一区二区三区在线看 | 国产v亚洲v欧美v专区 | 精品亚洲午夜久久久久 | 真人肉体一级毛片 | 一道本一区二区三区 | 高清国产一级精品毛片基地 | 婷婷综合丁香 | 97影院午夜午夜伦不卡 | 美国黄色片一级 | 国产小视频网址 | 精品国产一区二区三区四 | 成年女人a毛片免费视频 | 成人情趣视频无遮掩免费 |