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 装饰器,更进一步简化上下管理器的实现方式。

相关推荐
码云骑士5 分钟前
22-接手Django老项目(下)-读懂urls路由树与架构脉络
python·架构·django
码云骑士5 分钟前
29-Python-logging日志模块-print不是日志的生产级实战
开发语言·python
Attachment George13 分钟前
山东大学软件学院-项目实训-个人开发日志(十):材料问答链路开发——文档解析、OCR兜底与持续追问完善
python·ai·langchain·kotlin·rag
码云骑士14 分钟前
24-Django请求全链路-WSGI到数据库响应的完整旅程
数据库·python·django
贺国亚22 分钟前
06-奢侈零售VIP-Clienteling-Agent
开发语言·python·零售
knighthood200123 分钟前
鸿蒙PC迁移:jieba 中文分词 Python 三方库鸿蒙PC适配全记录
python·中文分词·harmonyos
nix.gnehc28 分钟前
Python 内存管理深度解析
开发语言·python
knight_9___38 分钟前
AI Agent 是什么?
人工智能·python·agent·rag·mcp
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第七章 Item 51)
开发语言·人工智能·笔记·python·学习方法
nix.gnehc1 小时前
Python 并发深度解析
服务器·开发语言·python