一顿花里胡哨猛如虎,结果我也不太懂
同时包含 __enter__() 和 __exit__() 方法的对象就是上下文管理器
with open('file_path', 'w') as file:
sys.exc_info()
with open('file_path', 'w') as file:
file.write('hello world !')
with open('file_path', 'w') as file:
open('file_path', 'w')
file.write('hello world !')
只需要给对象添加一个 __enter__ 和一个 __exit__ 方法
import sys
class Resource:
def __init__(self, name):
self.name = name
print("== 初始化方法 ==")
def __enter__(self):
print(f"** 进入上下文管理器自动调用:name is {self.name}")
# 可以返回任意类型的值
return {"name": self.name}
def __exit__(self, exc_type, exc_val, exc_tb):
print(f"## 退出上下文管理器自动调用:", sys.exc_info(), exc_type, exc_val, exc_tb)
if exc_tb is None:
print("没有异常时关闭资源")
else:
print("遇到异常时关闭资源")
也称为:使用 with ... as ... 管理资源
with Resource("小菠萝") as r:
print(r)
console 输出结果
== 初始化方法 ==
** 进入上下文管理器自动调用:name is 小菠萝
{'name': '小菠萝'}
## 退出上下文管理器自动调用: (None, None, None) None None None
没有异常时关闭资源
__exit__() 方法的三个参数值都是 None
with Resource("异常小菠萝") as r:
print('[with代码块] 异常之前的代码')
raise Exception("抛出了 Exception")
print('[with代码块] ~~~~~~~~异常之后的代码')
console 输出结果
== 初始化方法 ==
** 进入上下文管理器自动调用:name is 异常小菠萝
[with代码块] 异常之前的代码
## 退出上下文管理器自动调用: (<class 'Exception'>, Exception('抛出了 Exception'), <traceback object at 0x10e203200>) <class 'Exception'> 抛出了 Exception <traceback object at 0x10e203200>
遇到异常时关闭资源
Traceback (most recent call last):
File "/Users/polo/Documents/pylearn/第七章:文件相关/1_上下文管理器.py", line 36, in <module>
raise Exception("抛出了 Exception")
Exception: 抛出了 Exception
代码块抛出异常的时候,可以看到 __exit__() 方法的三个参数值的确来源于 sys.exc_info()
def __exit__(self, exc_type, exc_val, exc_tb):
print(f"## 退出上下文管理器自动调用:", sys.exc_info(), exc_type, exc_val, exc_tb)
if exc_tb is None:
print("没有异常时关闭资源")
else:
print("遇到异常时关闭资源")
return True
# 再次运行
with Resource("异常小菠萝") as r:
print('[with代码块] 抛出异常之前的代码')
raise Exception
print('[with代码块] 抛出异常之后的代码')
console 输出结果
== 初始化方法 ==
** 进入上下文管理器自动调用:name is 异常小菠萝
[with代码块] 异常之前的代码
## 退出上下文管理器自动调用: (<class 'Exception'>, Exception('抛出了 Exception'), <traceback object at 0x100e29200>) <class 'Exception'> 抛出了 Exception <traceback object at 0x100e29200>
遇到异常时关闭资源
不再抛出异常
通过装饰器 contextlib.contextmanager,来定义自己所需的基于生成器的上下文管理器
from contextlib import contextmanager
@contextmanager
def file_manager(name, mode):
try:
# 1、打开文件
file = open(name, mode)
# 2、返回文件资源对象
yield file
finally:
# 3、关闭文件
file.close()
with file_manager('a.txt', 'w') as file:
print(file)
file.write('hello world')