https://www.cnblogs.com/pyspark/articles/8819803.html
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001478651770626de401ff1c0d94f379774cabd842222ff000
总是在不同函数前后执行相同操作时
from contextlib import contextmanager
def my_test():
return 'zzz'
@contextmanager yield执行前后执行
def book_mark():
print(my_test())
print('《', end="")
yield
print('》', end="")
with book_mark():
# 核心代码
print('且将生活一饮而尽', end="")
----->
zzz
《且将生活一饮而尽》
class My_contextlib():
@contextmanager
def my_con1(self):
print('start')
yield
print('end')
def my_con2(self):
print('ok')
with My_contextlib().my_con1():
My_contextlib().my_con2()
print('no')
----->
start
ok
no
end