上下文管理器

上下文管理器:实现__enter__方法和__exit__方法的类就是一个上下文管理器:

示例代码:

from contextlib import ContextDecorator
class Mycontext(ContextDecorator):
    def __enter__(self):
        print('start')


    def __exit__(self, exc_type, exc_val, exc_tb):
        print('end')


@Mycontext()
def test():
    print('hello')

test()
原文地址:https://www.cnblogs.com/wjun0/p/11968155.html