wjun0

上下文管理器:实现__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()

 

分类:

技术点:

相关文章:

  • 2018-01-11
  • 2021-07-12
  • 2021-09-08
  • 2019-12-18
  • 2018-04-17
  • 2021-07-14
  • 2021-10-07
猜你喜欢
  • 2018-03-23
  • 2022-01-06
  • 2018-12-31
  • 2021-10-19
  • 2021-09-27
  • 2021-12-02
  • 2019-03-12
相关资源
相似解决方案