擅长:python、mysql、java
<p>从python文档中,我看到with是try/finally块的语法糖。
所以</p>
<pre><code>Is a file object "close" statement still needed in the second example, when the "with" statement is being used?
</code></pre>
<p>没有</p>
<p>从Python文档:</p>
<blockquote>
<p>The ‘with‘ statement clarifies code that previously would use
try...finally blocks to ensure that clean-up code is executed. In this
section, I’ll discuss the statement as it will commonly be used. In
the next section, I’ll examine the implementation details and show how
to write objects for use with this statement.</p>
<p>The ‘with‘ statement is a control-flow structure whose basic structure
is:</p>
<p>with expression [as variable]: with-block</p>
<p>The expression is evaluated, and it should result in an object that
supports the context management protocol (that is, has <strong>enter</strong>() and
<strong>exit</strong>() methods).</p>
</blockquote>
<p><a href="http://effbot.org/zone/python-with-statement.htm" rel="nofollow">Here</a>是另一篇清楚的文章。</p>