擅长:python、mysql、java
<p>似乎应该对多个上下文管理器实例使用<code>with</code>语句。</p>
<p>来自<a href="https://pymotw.com/2/contextlib/" rel="nofollow noreferrer">https://pymotw.com/2/contextlib/</a>:</p>
<blockquote>
<p>In Python 2.7 and later, nested() is deprecated because the with
statement supports nesting directly.</p>
</blockquote>
<pre><code>import contextlib
@contextlib.contextmanager
def make_context(name):
print 'entering:', name
yield name
print 'exiting :', name
with make_context('A') as A, make_context('B') as B, make_context('C') as C:
print 'inside with statement:', A, B, C
</code></pre>