擅长:python、mysql、java
<p>也许您没有一个足够新的Python标准库版本。看<a href="http://docs.python.org/library/tarfile.html" rel="nofollow">the docs</a>上面写着:</p>
<ul>
<li><strong><em>2.7版的新增功能:增加了对上下文管理器协议的支持。</em></strong></li>
</ul>
<p>顺便说一下,当您遇到在Python标准库中无法理解的行为时,这些文档应该是您的第一个资源。如果您理解整个“上下文管理”系统(对象协议)是相对较新的,并且可以通过Python中反常的搜索敌对<code>with</code>关键字进行访问,那么阅读该页面将回答您的问题。</p>
<p>这个对象协议实现了一些有用的隐式语义,包括关闭文件、释放锁以及其他清理和资源管理问题。。。即使面对例外。从根本上说,这是围绕<code>try:</code>…<code>finally:</code>的语义糖。。。有一些特性可以添加到类中以利用该支持。</p>
<p>要进一步理解这一点,最好阅读:<a href="http://docs.python.org/reference/datamodel.html?highlight=context%20manager#with-statement-context-managers" rel="nofollow">with Statement Context Managers</a>。</p>