Python 版的 try-with-resources -- with 上下文管理器
作为一个 Java 为母语的程序员来讲,学习起其他新的语言就难免任何事都与 Java 进行横向对比。Java 7 引入了能省去许多重复代码的 try-with-resources 特性,不用每回 try/finally 来释放资源(不便之处有局部变量必须声明在 try 之前,finally 里还要嵌套 try/catch 来处理异常)。比如下面的 Java 代码
1try(InputStream inputStream = new FileInputStream("abc.txt")) {
2 System.out.println(inputStream.read());
3} catch (Exception ex) {
4}它相应的不使用 try-with-resources 语法的代码就是
1InputStream inputStream = null;
2try {
3 inputStream = new FileInputStream("abc.txt");
4} catch (Exception ex) {
5} finally {
6 if(inputStream != null) {
7 try {
8 inputStream.close();
9 } catch (Exception ex) {
10 }
11 }
12}类似的 Python 也有自己的 try-with-resources 写法,就是 with 关键字,它的概念叫做上下文管理器(Context Manager)。
with 关键字的使用
1with open('some_file', 'w') as opened_file:
2 opened_file.write('Hola!')以上的代码相当于
1opened_file = open('some_file', 'w')
2try:
3 opened_file.write('Hola!')
4finally:
5 opened_file.close()也就是 with 关键字打开的资源会在 with 语句块结束后自动调用相应的方法自动释放(无论 with 中操作是否有异常)。
with 用起来是很方便的,但是什么样的资源可以用 with 关键字?Python 是怎么知道要调用哪个方法来关闭资源的?进而如何实现自己的支持上下文管理器的 Python 类。
再次回顾 Java 的 try-with-resources 语法,try(...) 括号支持的类必须是实现了 AutoCloseable 接口,它的接口方法是
public void close() throws IOException
也就是 Java 的 try-with-resources 语法会自动调用以上方法来释放资源,要实现可被自动释放的 Java 就只须遵照这一规则就行。
而在 Python 中,能被 with 的类有两种实现方式
实现基本方法以支持上下文管理器的类
一个 Python 类要能被用于 with 上下文,必须实现至少 __enter__ 和 __exit__ 方法。这两个方法的意思好理解,一个是创建资源后,后者是退出 with 语句块后。请看下面的例子
1class File(object):
2 def __init__(self, file_name, method):
3 self.file_obj = open(file_name, method)
4
5 def __enter__(self):
6 print("---enter")
7 return self.file_obj
8
9 def __exit__(self, type, value, traceback):
10 print("---exit")
11 self.file_obj.close()
12
13
14with File('data.txt', 'r') as data_file:
15 print(data_file.read())假设 data.txt 文件中的内容是
hello
world
那么以上程序执行后的输出就是
--enter
hello
world
---exit
__enter__返回的值作为with ... as data_file中的data_file变量的值,如果__enter__没有返回,data_file得到的就是NoneTypeobject 了。__exit__可利用来释放资源- 没有
__enter__方法试图用with的写法执行时会得到AttributeErro: __enter__异常 - 同样,没有
__exit__方法试图用with的写法执行时会得到AttributeErro: __exit__异常 __exit__有其他额外的三个参数,可获得资源的值,以及能处理with块中执行出现异常的情况__exit__的返回值也有用途,如果它返回True则出现的异常不再向外传播,其他值的话直接向外抛
利用生成器(Generator) 和装饰器创建支持上下文管理器的方法
此种方式比较简单,不过逻辑控制上没有这么强。
1from contextlib import contextmanager
2
3@contextmanager
4def open_file(name, method):
5 f = open(name, method)
6 yield f
7 f.close()使用 f 的执行代码将被放置在 yield f 所处的位置,with 使用以上方法。yield 后的 f 变量将是 with...as 后的变量值
1with open_file('some_file', 'w') as file_object:
2 file_object.write('hola!')这里也要注意异常处理的情况,比如把上面代码打开文件的模式换作 r, 仍然试图去写文件,这样在 open_file 方法的 yield f 位置将产生异常,会造成 f.close() 得不到执行,不能正确释放该资源。
欲更具防御性,前面的 yield f 可以扩展也如下的形式
1try:
2 yield f
3except Exception as ex:
4 pass #处理异常,或继续向外抛
5finally:
6 f.close()@contextmanager 装饰器内部也是封装为一个实现了 __enter__ 和 __exit__ 方法的对象。
参考链接:Context Managers
永久链接 https://yanbin.blog/python-try-with-resources-with-context-manager/, 来自 隔叶黄莺 Yanbin's Blog[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。