上下文管理器#

上下文管理器主要通过自定义魔术方法__enter____exit__实现。

enter and exit methods are used along with the ‘with’ block in python. The ‘with’ block in python is used to open and close the file object or any other objects that have a close method.

__exit__方法#

def __exit__(type, value, trace):
    return None

用于捕获异常。除了 self 之外,必须传入另外三个参数val,type 和 trace,分别表示 exception 的类型,值(如 IndexError: list index out of range 中,冒号后面的部分就是值),以及 traceback。

它的返回值是一个 boolean 对象:

  • 返回 True 则表示这个异常被忽略。

  • 返回 None, False 等则这个异常会抛出。

工具包contextlib#

contextlib.AbstractContextManager

参考资料#