【发布时间】:2015-07-22 18:24:02
【问题描述】:
在 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
【问题讨论】:
-
不知道为什么这被标记为重复。另一个帖子没有回答这个问题。我同意 Python 应该为 if 语句提出一个基于括号的语法。
标签: python with-statement contextmanager