上下文管理器

上下文管理器:實現__enter__方法和__exit__方法的類就是一個上下文管理器:spa

示例代碼:code

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()
相關文章
相關標籤/搜索