class aa():
    def bb(self):
        print("hhhh")
        return "hello world"

    def __enter__(self): # 必须有__enter__
        print("enter")
        return self

    def cc(self):
        print("www")

    def __exit__(self, exc_type, exc_val, exc_tb): # 必须有结束__exit__
        print("exit")

def ll():
    with aa() as a:
        return a.bb()

res = ll()
print(res)

 

结果:

enter
hhhh
exit
hello world

 

相关文章:

  • 2021-12-02
  • 2021-11-13
  • 2021-06-14
  • 2021-07-31
  • 2022-12-23
  • 2023-01-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
相关资源
相似解决方案