from contextlib import contextmanager
@contextmanager
def wfunc():
print("类似__enter__")
yield # 这里隔断__enter__和__exit__
print("类似__exit__")
with wfunc():
print("hello world")
>>> 类似__enter__
>>> hello world
>>> 类似__exit__
python contextlib
最新推荐文章于 2024-01-31 10:30:51 发布