上下文管理器的实现方法

上下文管理器允许你在有需要的时候,精确地分配和释放资源。使用上下文管理器最广泛的案例就是with语句了。
举个例子:

with open('some_file', 'w') as opened_file:
opened_file.write('Hello!')

上面这段代码打开了1个文件,往里面写入了一些数据,然后关闭该文件。如果在往文件写数据时发生了异常,它也会尝试去关闭文件。
上面那段代码与这段段是等价的:

file = open('some_file', 'w')
try:
  file.write('Hola!')
finally:
file.close()

当与第1个例子对比时,我们可以看到,通过使用with,许多样板代码(boilerplate code)被消掉了。 这就是with语句的主要优势,它确保我们的文件会被关闭,而不用关注嵌套代码如何退出。

上下文管理器的另一个1个常见用例,是资源的加锁和解锁。

下面我们来实现以下自己的上下文管理器

基于类实现

一个上下文管理器的类,最起码要定义__enter__和__exit__方法。

class File(object):
    def __init__(self, file_name, method):
        self.file_obj = open(file_name, method)
    def __enter__(self):
        return self.file_obj
    def __exit__(self, type, value, traceback):
        self.file_obj.close()

使用方法

with File('demo.txt', 'w') as opened_file:
    opened_file.write('hello!')

我们来看下底层做了什么

1. with语句先暂存了File类的__exit__方法
2. 然后它调用File类的__enter__方法
3. __enter__方法打开文件并返回给with语句
4. 打开的文件句柄被传递给opened_file参数
5. 我们使用.write()来写文件
6. with语句调用之前暂存的__exit__方法
7. __exit__方法关闭了文件

基于生成器实现

Python有个contextlib模块专门用于这个目的。我们可以使用1个生成器函数来实现1个上下文管理器,而不是使用1个类。

from contextlib import contextmanager

@contextmanager
def open_file(name):
    f = open(name, 'w')
    yield f
    f.close()

使用方法完全一样

with open_file('demo.txt', 'w') as f:
    f.write('hello!')
原文地址:https://www.cnblogs.com/hhsh/p/13590651.html