在Python 2.6中,我们通常这样格式化嵌套的上下文管理器:
with nested(
context1,
context2
) as a, b:
pass
自Python 2.7开始,nested
已经过时。我看到很多例子都是将多个上下文管理器放在一行上,但是我找不到允许它们跨多行的语法。你会如何做到这一点?# That's working fine
with context1 as a, context2 as b:
pass
# But how do we make it multine?
# These are not working
with (
context1,
context2
) as a, b:
pass
with context1 as a,
context2 as b:
pass