Python中with管理上下文

上下文管理器

上下文管理器本质就是能够支持with操作。

任何实现了**enter() 和 exit()**方法的对象都可称之为上下文管理器,上下文管理器对象可以使用 with 关键字。显然,文件(file)对象也实现了上下文管理器协议。

实现上下文管理器的另外方式

Python 还提供了一个contextmanager 的装饰器 ,更进一步简化了上下文管理器的实现方式。通过 yield 将函数分割成两部分,**yield 之前的语句在 enter 方法中执行,yield 之后的语句在 exit 方法中执行。**紧跟在 yield 后面的值是函数的返回值。

contextmanager装饰器的底层实现就是将yield之前的方法传入到__enter__方法中,将yield之后的方法传入到__exit__方法中

使用contextmanager装饰器需要导入模块

复制代码
from contextlib import contextmanager

使用__enter__和__exit__实现一个上下文管理器

读取同级目录下的hello文件的内容

复制代码
class my_open(object):
    def __init__(self, file_name, file_open_tyope):
        self.file_name = file_name
        self.file_open_type = file_open_tyope

    # 创建上文方法
    def __enter__(self):
        print("进入上文方法")
        self.file = open(self.file_name, self.file_open_type)
        return self.file

    # 创建下文方法
    def __exit__(self, exc_type, exc_val, exc_tb):
        print("进入下文方法")
        self.file.close()


def main():
    with my_open("hello", 'r') as file:
        result = file.read()
        print(result)


if __name__ == '__main__':
    main()

"""
进入上文方法
hello python
进入下文方法
"""

使用装饰器contextmanager实现一个上下文管理器

读取同级目录下的hello文件的内容

复制代码
from contextlib import contextmanager


@contextmanager
def my_open(file_name, file_open_type):
    # 进入上文操作
    print("进入上文")
    file = open(file_name, file_open_type)
    
    # 返回file,进行操作
    yield file
    
    # 进入下文操作
    print("进入下文")
    file.close()


if __name__ == '__main__':
    with my_open("hello", "r") as file:
        result = file.read()
        print(result)
"""
进入上文
hello python
进入下文
"""

总结

Python 提供了 with 语法用于简化资源操作的后续清除操作,实现原理建立在上下文管理器协议(实现__enter__和__exit__)之上

with使用代码中如果在打开过程中发生异常,需要使用try-except进行捕获

Python 还提供了一个 contextmanager 装饰器,更进一步简化上下管理器的实现方式。

相关推荐
嘘嘘出差12 分钟前
Python 爬虫入门实战:爬取豆瓣电影 Top 250
开发语言·爬虫·python
爱写代码的倒霉蛋17 分钟前
实现协程的三种方式
开发语言·python
崖边看雾41 分钟前
记录Python学习——第一章环境安装下载(Windows)
windows·python·学习·pycharm
用户298698530141 小时前
Python 实现 Excel 到 ODS、XPS、PostScript 及 PDF/A-1b 的格式转换
后端·python·excel
本地化文档1 小时前
xlwings-docs-l10n
python·github·excel·gitcode·sphinx
梅雅达编程笔记2 小时前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
heroboyluck2 小时前
AI工程师第四课 - 深度学习入门
人工智能·python·深度学习·llama
alicema11112 小时前
BRAIN 研究顾问(Research Consultant)兼职
python·量化
元Y亨H2 小时前
Python,单引号和双引号有何区别
python