0
我試着理解上下文管理器。 Peoples wtite,它相當於try...finally
區塊。上下文管理器的除了塊的返回值
是否存在能力用上下文管理器編寫下面的代碼?
def func():
try:
do_something1()
...
except Exception:
return False
do_something2()
return True
換句話說,如果發生異常,我會停止執行func
。
def func():
with context()
do_something1()
...
do_something2()
return True
你叫'do_something2'和''MyContextManager' do_something1'。這很奇怪。我在我的問題中添加了一個例子,我想要什麼。 – Liamax