登录
首页 >  文章 >  python教程

Python实现上下文协议,自定义with对象

时间:2026-05-19 18:50:51 159浏览 收藏

本文深入讲解了Python中上下文管理协议的核心机制——通过实现`__enter__`和`__exit__`方法,让自定义对象无缝支持`with`语句,实现资源的自动获取与安全释放;不仅剖析了协议执行流程(如返回值绑定、异常抑制逻辑),还通过计时器、安全文件操作等实用案例演示了手动实现类与`@contextmanager`装饰器两种高效方式,帮助开发者写出更简洁、健壮、符合Python惯用法的资源管理代码。

Python实现上下文协议_自定义with对象

Python 中的上下文管理协议(Context Management Protocol)通过 __enter____exit__ 两个特殊方法实现,让对象能用于 with 语句中,自动完成资源获取与释放,避免手动调用清理逻辑。

什么是上下文管理协议

只要一个类实现了 __enter____exit__ 方法,它就支持上下文管理协议。执行 with obj as x: 时:

  • __enter__ 在进入 with 块前被调用,返回值赋给 as 后的变量(若无 as,返回值被忽略);
  • __exit__(exc_type, exc_value, traceback) 在离开 with 块时被调用,无论是否发生异常都会执行,用于清理工作;
  • __exit__ 返回 True,会抑制异常(不向上抛出);返回 NoneFalse 则正常传播异常。

写一个简单的自定义 with 对象

比如封装一个计时器,在进入时记录开始时间,退出时打印耗时:

import time
<p>class Timer:
def <strong>init</strong>(self, name="block"):
self.name = name
self.start_time = None
self.end_time = None</p><pre class="brush:php;toolbar:false"><code>def __enter__(self):
    self.start_time = time.time()
    return self  # 可选:返回自身或任意值供 as 使用

def __exit__(self, exc_type, exc_value, traceback):
    self.end_time = time.time()
    elapsed = self.end_time - self.start_time
    print(f"[{self.name}] 执行耗时: {elapsed:.4f} 秒")
    # 不返回 True,不抑制异常</code>

使用方式:

with Timer("数据处理"):
    time.sleep(1.2)

输出类似:[数据处理] 执行耗时: 1.2005 秒

带异常处理的上下文管理器

如果希望在发生特定异常时静默处理(如忽略 FileNotFoundError),可在 __exit__ 中判断并返回 True

class SafeFileOpener:
    def __init__(self, filename, mode="r"):
        self.filename = filename
        self.mode = mode
        self.file = None
<pre class="brush:php;toolbar:false"><code>def __enter__(self):
    self.file = open(self.filename, self.mode)
    return self.file

def __exit__(self, exc_type, exc_value, traceback):
    if self.file:
        self.file.close()
    if exc_type is FileNotFoundError:
        print(f"警告:文件 {self.filename} 不存在,已跳过")
        return True  # 抑制 FileNotFoundError
    return False  # 其他异常照常抛出</code>

这样写就不会因文件不存在而中断程序:

with SafeFileOpener("missing.txt") as f:
    content = f.read()  # 若文件不存在,只打印警告,不报错

更简洁的方式:用 contextlib.contextmanager

对于简单逻辑,无需写完整类,可用装饰器 @contextmanager 将生成器函数转为上下文管理器:

from contextlib import contextmanager
<p>@contextmanager
def timer(name="block"):
start = time.time()
try:
yield  # 进入 with 块的位置
finally:
end = time.time()
print(f"[{name}] 耗时: {end - start:.4f} 秒")</p><h1>使用方式相同</h1><p>with timer("循环计算"):
sum(i * i for i in range(10**6))
</p>

yield 之前是 __enter__,之后(含 finally)是 __exit__ 的等价逻辑,更轻量易读。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Python实现上下文协议,自定义with对象》文章吧,也可关注golang学习网公众号了解相关技术文章。

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>