Files
hustoj/web/python-docs/whatsnew/2.4.html
2025-03-09 17:10:56 +08:00

1634 lines
166 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Python 2.4 有什么新变化 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="Python 2.3 有什么新变化" href="2.3.html" />
<link rel="prev" title="Python 2.5 有什么新变化" href="2.5.html" />
<link rel="canonical" href="https://docs.python.org/3/whatsnew/2.4.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Python 2.4 有什么新变化</a><ul>
<li><a class="reference internal" href="#pep-218-built-in-set-objects">PEP 218: 内置集合对象</a></li>
<li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: 统一长整数和整数</a></li>
<li><a class="reference internal" href="#pep-289-generator-expressions">PEP 289: 生成器表达式</a></li>
<li><a class="reference internal" href="#pep-292-simpler-string-substitutions">PEP 292: 更简单的字符串替换</a></li>
<li><a class="reference internal" href="#pep-318-decorators-for-functions-and-methods">PEP 318: 函数和方法的装饰器</a></li>
<li><a class="reference internal" href="#pep-322-reverse-iteration">PEP 322: 反向迭代</a></li>
<li><a class="reference internal" href="#pep-324-new-subprocess-module">PEP 324: 新的子进程模块</a></li>
<li><a class="reference internal" href="#pep-327-decimal-data-type">PEP 327: 十进制数据类型</a><ul>
<li><a class="reference internal" href="#why-is-decimal-needed">为什么需要十进制?</a></li>
<li><a class="reference internal" href="#the-decimal-type"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> 类型</a></li>
<li><a class="reference internal" href="#the-context-type"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> 类型</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-328-multi-line-imports">PEP 328: 多行导入</a></li>
<li><a class="reference internal" href="#pep-331-locale-independent-float-string-conversions">PEP 331: Locale-Independent Float/String Conversions</a></li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a><ul>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a><ul>
<li><a class="reference internal" href="#cookielib">cookielib</a></li>
<li><a class="reference internal" href="#doctest">doctest</a></li>
</ul>
</li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a><ul>
<li><a class="reference internal" href="#port-specific-changes">移植专属的改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-2-4">移植到 Python 2.4</a></li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="2.5.html"
title="上一章">Python 2.5 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="2.3.html"
title="下一章">Python 2.3 有什么新变化</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.4.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/whatsnew/2.4.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="2.3.html" title="Python 2.3 有什么新变化"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="2.5.html" title="Python 2.5 有什么新变化"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python的新变化</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="what-s-new-in-python-2-4">
<h1>Python 2.4 有什么新变化<a class="headerlink" href="#what-s-new-in-python-2-4" title="永久链接至标题"></a></h1>
<dl class="field-list simple">
<dt class="field-odd">作者</dt>
<dd class="field-odd"><p>A.M. Kuchling</p>
</dd>
</dl>
<p>本文介绍了2005年3月30日发布的 Python 2.4.1 的新功能。</p>
<p>Python 2.4 是一个中等规模的发布版。 它引入的变化没有激进的 Python 2.2 那么多,但比保守的 2.3 发布版引入了更多的特性。 最主要的新语言特性是函数装饰器和生成器表达式;其他大部分改动都是针对标准库。</p>
<p>根据 CVS 变更日志Python 2.3 和 2.4 之间共应用了 481 个补丁并修复了 502 个错误。 这两个数字可能都被低估了。</p>
<p>本文并不试图提供每一个新特性的完整规范说明,而是对每个特性进行简要的介绍。 要了解完整细节,你应该参考 Python 2.4 的文档,如 Python 库参考和 Python 参考手册等。 通常你需要参阅特定新特性的 PEP 以了解有关具体实现和设计原理的说明。</p>
<section id="pep-218-built-in-set-objects">
<h2>PEP 218: 内置集合对象<a class="headerlink" href="#pep-218-built-in-set-objects" title="永久链接至标题"></a></h2>
<p>Python 2.3 引入了 <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code> 模块。 现在集合数据类型的 C 语言实现作为两个新的内置类型 <code class="docutils literal notranslate"><span class="pre">set(iterable)</span></code><code class="docutils literal notranslate"><span class="pre">frozenset(iterable)</span></code> 被添加到 Python 内核中。 它们为成员测试、消除序列中的重复数据以及并集、交集、差集和对称差集等数学运算提供了高速操作。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span> <span class="c1"># form a set from a string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;z&#39;</span> <span class="ow">in</span> <span class="n">a</span> <span class="c1"># fast membership testing</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="c1"># unique letters in a</span>
<span class="go">set([&#39;a&#39;, &#39;r&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1"># convert back into a string</span>
<span class="go">&#39;arbcd&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;alacazam&#39;</span><span class="p">)</span> <span class="c1"># form a second set</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="c1"># letters in a but not in b</span>
<span class="go">set([&#39;r&#39;, &#39;d&#39;, &#39;b&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">|</span> <span class="n">b</span> <span class="c1"># letters in either a or b</span>
<span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">&amp;</span> <span class="n">b</span> <span class="c1"># letters in both a and b</span>
<span class="go">set([&#39;a&#39;, &#39;c&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">^</span> <span class="n">b</span> <span class="c1"># letters in a or b but not both</span>
<span class="go">set([&#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">)</span> <span class="c1"># add a new element</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s1">&#39;wxy&#39;</span><span class="p">)</span> <span class="c1"># add multiple new elements</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;, &#39;r&#39;, &#39;w&#39;, &#39;y&#39;, &#39;x&#39;, &#39;z&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span> <span class="c1"># take one element out</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">set([&#39;a&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;, &#39;r&#39;, &#39;w&#39;, &#39;y&#39;, &#39;z&#39;])</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-func docutils literal notranslate"><span class="pre">frozenset()</span></code></a> 类型是 <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> 的不可变版本。 由于它是不可变且不可哈希的对象,因而可被用作字典的键或另一个集合的成员。</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code> 模块仍被保留在标准库中,如果你想要子类化 <code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code><code class="xref py py-class docutils literal notranslate"><span class="pre">ImmutableSet</span></code> 类时会很有用处。 目前还没有弃用该模块的计划。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-29"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0218"><strong>PEP 218</strong></a> - 添加内置Set对象类型</dt><dd><p>最初由 Greg Wilson 提出,由 Raymond Hettinger 最终实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-237-unifying-long-integers-and-integers">
<h2>PEP 237: 统一长整数和整数<a class="headerlink" href="#pep-237-unifying-long-integers-and-integers" title="永久链接至标题"></a></h2>
<p>The lengthy transition process for this PEP, begun in Python 2.2, takes another
step forward in Python 2.4. In 2.3, certain integer operations that would
behave differently after int/long unification triggered <a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a>
warnings and returned values limited to 32 or 64 bits (depending on your
platform). In 2.4, these expressions no longer produce a warning and instead
produce a different result that's usually a long integer.</p>
<p>The problematic expressions are primarily left shifts and lengthy hexadecimal
and octal constants. For example, <code class="docutils literal notranslate"><span class="pre">2</span> <span class="pre">&lt;&lt;</span> <span class="pre">32</span></code> results in a warning in 2.3,
evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns
the correct answer, 8589934592.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-30"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0237"><strong>PEP 237</strong></a> - 统一长整数和整数</dt><dd><p>原始PEP由 Moshe Zadka 和 GvR 撰写2.4 的变更由 Kalle Svensson 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-289-generator-expressions">
<h2>PEP 289: 生成器表达式<a class="headerlink" href="#pep-289-generator-expressions" title="永久链接至标题"></a></h2>
<p>The iterator feature introduced in Python 2.2 and the <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module
make it easier to write programs that loop through large data sets without
having the entire data set in memory at one time. List comprehensions don't fit
into this picture very well because they produce a Python list object containing
all of the items. This unavoidably pulls all of the objects into memory, which
can be a problem if your data set is very large. When trying to write a
functionally-styled program, it would be natural to write something like:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">links</span> <span class="o">=</span> <span class="p">[</span><span class="n">link</span> <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">]</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span>
<span class="o">...</span>
</pre></div>
</div>
<p>代替:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">():</span>
<span class="k">if</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">:</span>
<span class="k">continue</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The first form is more concise and perhaps more readable, but if you're dealing
with a large number of link objects you'd have to write the second form to avoid
having all link objects in memory at the same time.</p>
<p>Generator expressions work similarly to list comprehensions but don't
materialize the entire list; instead they create a generator that will return
elements one by one. The above example could be written as:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">links</span> <span class="o">=</span> <span class="p">(</span><span class="n">link</span> <span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">get_all_links</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">link</span><span class="o">.</span><span class="n">followed</span><span class="p">)</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">links</span><span class="p">:</span>
<span class="o">...</span>
</pre></div>
</div>
<p>Generator expressions always have to be written inside parentheses, as in the
above example. The parentheses signalling a function call also count, so if you
want to create an iterator that will be immediately passed to a function you
could write:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="nb">sum</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">count</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">list_all_objects</span><span class="p">())</span>
</pre></div>
</div>
<p>Generator expressions differ from list comprehensions in various small ways.
Most notably, the loop variable (<em>obj</em> in the above example) is not accessible
outside of the generator expression. List comprehensions leave the variable
assigned to its last value; future versions of Python will change this, making
list comprehensions match generator expressions in this respect.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-31"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0289"><strong>PEP 289</strong></a> - 生成器表达式</dt><dd><p>Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts
steered by Hye-Shik Chang.</p>
</dd>
</dl>
</div>
</section>
<section id="pep-292-simpler-string-substitutions">
<h2>PEP 292: 更简单的字符串替换<a class="headerlink" href="#pep-292-simpler-string-substitutions" title="永久链接至标题"></a></h2>
<p>Some new classes in the standard library provide an alternative mechanism for
substituting variables into strings; this style of substitution may be better
for applications where untrained users need to edit templates.</p>
<p>按名称替换变量的常用方式是 <code class="docutils literal notranslate"><span class="pre">%</span></code> 运算符:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">%(page)i</span><span class="s1">: </span><span class="si">%(title)s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;The Best of Times&#39;</span><span class="p">}</span>
<span class="go">&#39;2: The Best of Times&#39;</span>
</pre></div>
</div>
<p>When writing the template string, it can be easy to forget the <code class="docutils literal notranslate"><span class="pre">i</span></code> or <code class="docutils literal notranslate"><span class="pre">s</span></code>
after the closing parenthesis. This isn't a big problem if the template is in a
Python module, because you run the code, get an &quot;Unsupported format character&quot;
<a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, and fix the problem. However, consider an application such
as Mailman where template strings or translations are being edited by users who
aren't aware of the Python language. The format string's syntax is complicated
to explain to such users, and if they make a mistake, it's difficult to provide
helpful feedback to them.</p>
<p>PEP 292 给 <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> 模块增加了一个 <code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code>,它使用 <code class="docutils literal notranslate"><span class="pre">$</span></code> 来表示替换:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$page: $title&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">substitute</span><span class="p">({</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="s1">&#39;The Best of Times&#39;</span><span class="p">})</span>
<span class="go">&#39;2: The Best of Times&#39;</span>
</pre></div>
</div>
<p>如果某个键在字典中找不到,<code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code> 方法将引发 <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>。 还有一个 <code class="xref py py-meth docutils literal notranslate"><span class="pre">safe_substitute()</span></code> 方法则会忽略找不到的键:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$page: $title&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">safe_substitute</span><span class="p">({</span><span class="s1">&#39;page&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">})</span>
<span class="go">&#39;3: $title&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-32"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0292"><strong>PEP 292</strong></a> - 更简单的字符串替换</dt><dd><p>由 Barry Warsaw 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-318-decorators-for-functions-and-methods">
<h2>PEP 318: 函数和方法的装饰器<a class="headerlink" href="#pep-318-decorators-for-functions-and-methods" title="永久链接至标题"></a></h2>
<p>Python 2.2 extended Python's object model by adding static methods and class
methods, but it didn't extend Python's syntax to provide any new way of defining
static or class methods. Instead, you had to write a <a class="reference internal" href="../reference/compound_stmts.html#def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">def</span></code></a> statement
in the usual way, and pass the resulting method to a <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> or
<a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> function that would wrap up the function as a method of the
new type. Your code would look like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">meth</span> <span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
<span class="o">...</span>
<span class="n">meth</span> <span class="o">=</span> <span class="nb">classmethod</span><span class="p">(</span><span class="n">meth</span><span class="p">)</span> <span class="c1"># Rebind name to wrapped-up class method</span>
</pre></div>
</div>
<p>If the method was very long, it would be easy to miss or forget the
<a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> invocation after the function body.</p>
<p>The intention was always to add some syntax to make such definitions more
readable, but at the time of 2.2's release a good syntax was not obvious. Today
a good syntax <em>still</em> isn't obvious but users are asking for easier access to
the feature; a new syntactic feature has been added to meet this need.</p>
<p>The new feature is called &quot;function decorators&quot;. The name comes from the idea
that <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a>, <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a>, and friends are storing
additional information on a function object; they're <em>decorating</em> functions with
more details.</p>
<p>The notation borrows from Java and uses the <code class="docutils literal notranslate"><span class="pre">'&#64;'</span></code> character as an indicator.
Using the new syntax, the example above would be written:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">meth</span> <span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> is shorthand for the <code class="docutils literal notranslate"><span class="pre">meth=classmethod(meth)</span></code> assignment.
More generally, if you have the following:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@A</span>
<span class="nd">@B</span>
<span class="nd">@C</span>
<span class="k">def</span> <span class="nf">f</span> <span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>它等价于以下无装饰器的代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="o">...</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">A</span><span class="p">(</span><span class="n">B</span><span class="p">(</span><span class="n">C</span><span class="p">(</span><span class="n">f</span><span class="p">)))</span>
</pre></div>
</div>
<p>Decorators must come on the line before a function definition, one decorator per
line, and can't be on the same line as the def statement, meaning that <code class="docutils literal notranslate"><span class="pre">&#64;A</span> <span class="pre">def</span>
<span class="pre">f():</span> <span class="pre">...</span></code> is illegal. You can only decorate function definitions, either at
the module level or inside a class; you can't decorate class definitions.</p>
<p>A decorator is just a function that takes the function to be decorated as an
argument and returns either the same function or some new object. The return
value of the decorator need not be callable (though it typically is), unless
further decorators will be applied to the result. It's easy to write your own
decorators. The following simple example just sets an attribute on the function
object:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">deco</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">func</span><span class="o">.</span><span class="n">attr</span> <span class="o">=</span> <span class="s1">&#39;decorated&#39;</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">func</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@deco</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span>
<span class="go">&lt;function f at 0x402ef0d4&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">attr</span>
<span class="go">&#39;decorated&#39;</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>As a slightly more realistic example, the following decorator checks that the
supplied argument is an integer:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">require_int</span> <span class="p">(</span><span class="n">func</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">wrapper</span> <span class="p">(</span><span class="n">arg</span><span class="p">):</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
<span class="k">return</span> <span class="n">wrapper</span>
<span class="nd">@require_int</span>
<span class="k">def</span> <span class="nf">p1</span> <span class="p">(</span><span class="n">arg</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">arg</span>
<span class="nd">@require_int</span>
<span class="k">def</span> <span class="nf">p2</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">arg</span><span class="o">*</span><span class="mi">2</span>
</pre></div>
</div>
<p>An example in <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0318"><strong>PEP 318</strong></a> contains a fancier version of this idea that lets you
both specify the required type and check the returned type.</p>
<p>Decorator functions can take arguments. If arguments are supplied, your
decorator function is called with only those arguments and must return a new
decorator function; this function must take a single function and return a
function, as previously described. In other words, <code class="docutils literal notranslate"><span class="pre">&#64;A</span> <span class="pre">&#64;B</span> <span class="pre">&#64;C(args)</span></code> becomes:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="o">...</span>
<span class="n">_deco</span> <span class="o">=</span> <span class="n">C</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="n">A</span><span class="p">(</span><span class="n">B</span><span class="p">(</span><span class="n">_deco</span><span class="p">(</span><span class="n">f</span><span class="p">)))</span>
</pre></div>
</div>
<p>Getting this right can be slightly brain-bending, but it's not too difficult.</p>
<p>A small related change makes the <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_name</span></code> attribute of functions
writable. This attribute is used to display function names in tracebacks, so
decorators should change the name of any new function that's constructed and
returned.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-33"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0318"><strong>PEP 318</strong></a> - 函数、方法和类的装饰器</dt><dd><p>Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people
wrote patches implementing function decorators, but the one that was actually
checked in was patch #979728, written by Mark Russell.</p>
</dd>
<dt><a class="reference external" href="https://wiki.python.org/moin/PythonDecoratorLibrary">https://wiki.python.org/moin/PythonDecoratorLibrary</a></dt><dd><p>该Wiki页面包含几个装饰器示例。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-322-reverse-iteration">
<h2>PEP 322: 反向迭代<a class="headerlink" href="#pep-322-reverse-iteration" title="永久链接至标题"></a></h2>
<p>A new built-in function, <code class="docutils literal notranslate"><span class="pre">reversed(seq)</span></code>, takes a sequence and returns an
iterator that loops over the elements of the sequence in reverse order.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">xrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">)):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">i</span>
<span class="gp">...</span>
<span class="go">3</span>
<span class="go">2</span>
<span class="go">1</span>
</pre></div>
</div>
<p>Compared to extended slicing, such as <code class="docutils literal notranslate"><span class="pre">range(1,4)[::-1]</span></code>, <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> is
easier to read, runs faster, and uses substantially less memory.</p>
<p>Note that <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> only accepts sequences, not arbitrary iterators. If
you want to reverse an iterator, first convert it to a list with <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a>.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">input</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/etc/passwd&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">input</span><span class="p">)):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">line</span>
<span class="gp">...</span>
<span class="go">root:*:0:0:System Administrator:/var/root:/bin/tcsh</span>
<span class="go"> ...</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-34"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0322"><strong>PEP 322</strong></a> - 反向迭代</dt><dd><p>由 Raymond Hettinger 撰写并实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-324-new-subprocess-module">
<h2>PEP 324: 新的子进程模块<a class="headerlink" href="#pep-324-new-subprocess-module" title="永久链接至标题"></a></h2>
<p>The standard library provides a number of ways to execute a subprocess, offering
different features and different levels of complexity.
<code class="docutils literal notranslate"><span class="pre">os.system(command)</span></code> is easy to use, but slow (it runs a shell process
which executes the command) and dangerous (you have to be careful about escaping
the shell's metacharacters). The <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code> module offers classes that can
capture standard output and standard error from the subprocess, but the naming
is confusing. The <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module cleans this up, providing a unified
interface that offers all the features you might need.</p>
<p>Instead of <code class="xref py py-mod docutils literal notranslate"><span class="pre">popen2</span></code>'s collection of classes, <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> contains a
single class called <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code> whose constructor supports a number of
different keyword arguments.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">bufsize</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">executable</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">stdin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">preexec_fn</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">close_fds</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">cwd</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">env</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">universal_newlines</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">startupinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">creationflags</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
</pre></div>
</div>
<p><em>args</em> is commonly a sequence of strings that will be the arguments to the
program executed as the subprocess. (If the <em>shell</em> argument is true, <em>args</em>
can be a string which will then be passed on to the shell for interpretation,
just as <a class="reference internal" href="../library/os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a> does.)</p>
<p><em>stdin</em>, <em>stdout</em>, and <em>stderr</em> specify what the subprocess's input, output, and
error streams will be. You can provide a file object or a file descriptor, or
you can use the constant <code class="docutils literal notranslate"><span class="pre">subprocess.PIPE</span></code> to create a pipe between the
subprocess and the parent.</p>
<p id="index-7">此构造器有几个方便的选项:</p>
<ul class="simple">
<li><p><em>close_fds</em> 将在运行子进程之前请求关闭所有文件描述符。</p></li>
<li><p><em>cwd</em> 指定执行子进程将使用的工作目录(默认为父进程的工作目录)。</p></li>
<li><p><em>env</em> 是一个指定环境变量的字典。</p></li>
<li><p><em>preexec_fn</em> 是将在子进程启动之前被调用的函数。</p></li>
<li><p><em>universal_newlines</em> opens the child's input and output using Python's
<a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a> feature.</p></li>
</ul>
<p>Once you've created the <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code> instance, you can call its <code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code>
method to pause until the subprocess has exited, <code class="xref py py-meth docutils literal notranslate"><span class="pre">poll()</span></code> to check if it's
exited without pausing, or <code class="docutils literal notranslate"><span class="pre">communicate(data)</span></code> to send the string <em>data</em>
to the subprocess's standard input. <code class="docutils literal notranslate"><span class="pre">communicate(data)</span></code> then reads any
data that the subprocess has sent to its standard output or standard error,
returning a tuple <code class="docutils literal notranslate"><span class="pre">(stdout_data,</span> <span class="pre">stderr_data)</span></code>.</p>
<p><code class="xref py py-func docutils literal notranslate"><span class="pre">call()</span></code> is a shortcut that passes its arguments along to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code>
constructor, waits for the command to complete, and returns the status code of
the subprocess. It can serve as a safer analog to <a class="reference internal" href="../library/os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sts</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">([</span><span class="s1">&#39;dpkg&#39;</span><span class="p">,</span> <span class="s1">&#39;-i&#39;</span><span class="p">,</span> <span class="s1">&#39;/tmp/new-package.deb&#39;</span><span class="p">])</span>
<span class="k">if</span> <span class="n">sts</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="c1"># Success</span>
<span class="o">...</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># dpkg returned an error</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The command is invoked without use of the shell. If you really do want to use
the shell, you can add <code class="docutils literal notranslate"><span class="pre">shell=True</span></code> as a keyword argument and provide a string
instead of a sequence:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sts</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="s1">&#39;dpkg -i /tmp/new-package.deb&#39;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>The PEP takes various examples of shell and Python code and shows how they'd be
translated into Python code that uses <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a>. Reading this section
of the PEP is highly recommended.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-35"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0324"><strong>PEP 324</strong></a> - 子进程 - 新的进程模块</dt><dd><p>由 Peter Åstrand 在 Fredrik Lundh 等人的协助下撰写并实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-327-decimal-data-type">
<h2>PEP 327: 十进制数据类型<a class="headerlink" href="#pep-327-decimal-data-type" title="永久链接至标题"></a></h2>
<p>Python has always supported floating-point (FP) numbers, based on the underlying
C <code class="xref c c-type docutils literal notranslate"><span class="pre">double</span></code> type, as a data type. However, while most programming
languages provide a floating-point type, many people (even programmers) are
unaware that floating-point numbers don't represent certain decimal fractions
accurately. The new <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> type can represent these fractions
accurately, up to a user-specified precision limit.</p>
<section id="why-is-decimal-needed">
<h3>为什么需要十进制?<a class="headerlink" href="#why-is-decimal-needed" title="永久链接至标题"></a></h3>
<p>The limitations arise from the representation used for floating-point numbers.
FP numbers are made up of three components:</p>
<ul class="simple">
<li><p>The sign, which is positive or negative.</p></li>
<li><p>The mantissa, which is a single-digit binary number followed by a fractional
part. For example, <code class="docutils literal notranslate"><span class="pre">1.01</span></code> in base-2 notation is <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">+</span> <span class="pre">0/2</span> <span class="pre">+</span> <span class="pre">1/4</span></code>, or 1.25 in
decimal notation.</p></li>
<li><p>The exponent, which tells where the decimal point is located in the number
represented.</p></li>
</ul>
<p>For example, the number 1.25 has positive sign, a mantissa value of 1.01 (in
binary), and an exponent of 0 (the decimal point doesn't need to be shifted).
The number 5 has the same sign and mantissa, but the exponent is 2 because the
mantissa is multiplied by 4 (2 to the power of the exponent 2); 1.25 * 4 equals
5.</p>
<p>Modern systems usually provide floating-point support that conforms to a
standard called IEEE 754. C's <code class="xref c c-type docutils literal notranslate"><span class="pre">double</span></code> type is usually implemented as a
64-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This
means that numbers can only be specified to 52 bits of precision. If you're
trying to represent numbers whose expansion repeats endlessly, the expansion is
cut off after 52 bits. Unfortunately, most software needs to produce output in
base 10, and common fractions in base 10 are often repeating decimals in binary.
For example, 1.1 decimal is binary <code class="docutils literal notranslate"><span class="pre">1.0001100110011</span> <span class="pre">...</span></code>; .1 = 1/16 + 1/32 +
1/256 plus an infinite number of additional terms. IEEE 754 has to chop off
that infinitely repeated decimal after 52 digits, so the representation is
slightly inaccurate.</p>
<p>Sometimes you can see this inaccuracy when the number is printed:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mf">1.1</span>
<span class="go">1.1000000000000001</span>
</pre></div>
</div>
<p>The inaccuracy isn't always visible when you print the number because the
FP-to-decimal-string conversion is provided by the C library, and most C libraries try
to produce sensible output. Even if it's not displayed, however, the inaccuracy
is still there and subsequent operations can magnify the error.</p>
<p>For many applications this doesn't matter. If I'm plotting points and
displaying them on my monitor, the difference between 1.1 and 1.1000000000000001
is too small to be visible. Reports often limit output to a certain number of
decimal places, and if you round the number to two or three or even eight
decimal places, the error is never apparent. However, for applications where it
does matter, it's a lot of work to implement your own custom arithmetic
routines.</p>
<p>因此,创建了 <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> 类型。</p>
</section>
<section id="the-decimal-type">
<h3><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> 类型<a class="headerlink" href="#the-decimal-type" title="永久链接至标题"></a></h3>
<p>A new module, <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a>, was added to Python's standard library. It
contains two classes, <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code>. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>
instances represent numbers, and <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> instances are used to wrap up
various settings such as the precision and default rounding mode.</p>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances are immutable, like regular Python integers and FP
numbers; once it's been created, you can't change the value an instance
represents. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances can be created from integers or
strings:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">decimal</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1972</span><span class="p">)</span>
<span class="go">Decimal(&quot;1972&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">&quot;1.1&quot;</span><span class="p">)</span>
<span class="go">Decimal(&quot;1.1&quot;)</span>
</pre></div>
</div>
<p>You can also provide tuples containing the sign, the mantissa represented as a
tuple of decimal digits, and the exponent:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="o">-</span><span class="mi">2</span><span class="p">))</span>
<span class="go">Decimal(&quot;-14.75&quot;)</span>
</pre></div>
</div>
<p>Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is
negative.</p>
<p>Converting from floating-point numbers poses a bit of a problem: should the FP
number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
plus whatever inaccuracies are introduced? The decision was to dodge the issue
and leave such a conversion out of the API. Instead, you should convert the
floating-point number into a string using the desired precision and pass the
string to the <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> constructor:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="mf">1.1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
<span class="go">Decimal(&quot;1.1&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%.12f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">f</span><span class="p">)</span>
<span class="go">Decimal(&quot;1.100000000000&quot;)</span>
</pre></div>
</div>
<p>Once you have <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances, you can perform the usual mathematical
operations on them. One limitation: exponentiation requires an integer
exponent:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;35.72&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;1.73&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="go">Decimal(&quot;37.45&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">-</span><span class="n">b</span>
<span class="go">Decimal(&quot;33.99&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">*</span><span class="n">b</span>
<span class="go">Decimal(&quot;61.7956&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">/</span><span class="n">b</span>
<span class="go">Decimal(&quot;20.64739884393063583815028902&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">**</span> <span class="mi">2</span>
<span class="go">Decimal(&quot;1275.9184&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">**</span><span class="n">b</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="w"> </span><span class="c">...</span>
<span class="gr">decimal.InvalidOperation</span>: <span class="n">x ** (non-integer)</span>
</pre></div>
</div>
<p>You can combine <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances with integers, but not with
floating-point numbers:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+</span> <span class="mi">4</span>
<span class="go">Decimal(&quot;39.72&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+</span> <span class="mf">4.5</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="w"> </span><span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">You can interact Decimal only with int, long or Decimal data types.</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> numbers can be used with the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> and <a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code></a>
modules, but note that they'll be immediately converted to floating-point
numbers before the operation is performed, resulting in a possible loss of
precision and accuracy. You'll also get back a regular floating-point number
and not a <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span><span class="o">,</span> <span class="nn">cmath</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">&#39;123456789012.345&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">351364.18288201344</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="n">d</span><span class="p">)</span>
<span class="go">351364.18288201344j</span>
</pre></div>
</div>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> instances have a <code class="xref py py-meth docutils literal notranslate"><span class="pre">sqrt()</span></code> method that returns a
<code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>, but if you need other things such as trigonometric functions
you'll have to implement them.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>
<span class="go">Decimal(&quot;351364.1828820134592177245001&quot;)</span>
</pre></div>
</div>
</section>
<section id="the-context-type">
<h3><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> 类型<a class="headerlink" href="#the-context-type" title="永久链接至标题"></a></h3>
<p>Instances of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> class encapsulate several settings for
decimal operations:</p>
<ul class="simple">
<li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">prec</span></code> is the precision, the number of decimal places.</p></li>
<li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">rounding</span></code> specifies the rounding mode. The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module has
constants for the various possibilities: <code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_DOWN</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_CEILING</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ROUND_HALF_EVEN</span></code>, and various others.</p></li>
<li><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">traps</span></code> is a dictionary specifying what happens on encountering certain
error conditions: either an exception is raised or a value is returned. Some
examples of error conditions are division by zero, loss of precision, and
overflow.</p></li>
</ul>
<p>There's a thread-local default context available by calling <code class="xref py py-func docutils literal notranslate"><span class="pre">getcontext()</span></code>;
you can change the properties of this context to alter the default precision,
rounding, or trap handling. The following example shows the effect of changing
the precision of the default context:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span>
<span class="go">28</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="go">Decimal(&quot;0.1428571428571428571428571429&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">9</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="go">Decimal(&quot;0.142857143&quot;)</span>
</pre></div>
</div>
<p>The default action for error conditions is selectable; the module can either
return a special value such as infinity or not-a-number, or exceptions can be
raised:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="w"> </span><span class="c">...</span>
<span class="gr">decimal.DivisionByZero</span>: <span class="n">x / 0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">traps</span><span class="p">[</span><span class="n">decimal</span><span class="o">.</span><span class="n">DivisionByZero</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">Decimal(&quot;Infinity&quot;)</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> instance also has various methods for formatting numbers
such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">to_eng_string()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">to_sci_string()</span></code>.</p>
<p>For more information, see the documentation for the <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module, which
includes a quick-start tutorial and a reference.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-36"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0327"><strong>PEP 327</strong></a> - 十进数据类型</dt><dd><p>由 Facundo Batista 撰写由Facundo Batista, Eric Price, Raymond Hettinger, Aahz 和 Tim Peters 实现。</p>
</dd>
<dt><a class="reference external" href="http://www.lahey.com/float.htm">http://www.lahey.com/float.htm</a></dt><dd><p>The article uses Fortran code to illustrate many of the problems that
floating-point inaccuracy can cause.</p>
</dd>
<dt><a class="reference external" href="http://speleotrove.com/decimal/">http://speleotrove.com/decimal/</a></dt><dd><p>A description of a decimal-based representation. This representation is being
proposed as a standard, and underlies the new Python decimal type. Much of this
material was written by Mike Cowlishaw, designer of the Rexx language.</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="pep-328-multi-line-imports">
<h2>PEP 328: 多行导入<a class="headerlink" href="#pep-328-multi-line-imports" title="永久链接至标题"></a></h2>
<p>One language change is a small syntactic tweak aimed at making it easier to
import many names from a module. In a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">names</span></code> statement,
<em>names</em> is a sequence of names separated by commas. If the sequence is very
long, you can either write multiple imports from the same module, or you can use
backslashes to escape the line endings like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">SimpleXMLRPCServer</span> <span class="kn">import</span> <span class="n">SimpleXMLRPCServer</span><span class="p">,</span>\
<span class="n">SimpleXMLRPCRequestHandler</span><span class="p">,</span>\
<span class="n">CGIXMLRPCRequestHandler</span><span class="p">,</span>\
<span class="n">resolve_dotted_attribute</span>
</pre></div>
</div>
<p>The syntactic change in Python 2.4 simply allows putting the names within
parentheses. Python ignores newlines within a parenthesized expression, so the
backslashes are no longer needed:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">SimpleXMLRPCServer</span> <span class="kn">import</span> <span class="p">(</span><span class="n">SimpleXMLRPCServer</span><span class="p">,</span>
<span class="n">SimpleXMLRPCRequestHandler</span><span class="p">,</span>
<span class="n">CGIXMLRPCRequestHandler</span><span class="p">,</span>
<span class="n">resolve_dotted_attribute</span><span class="p">)</span>
</pre></div>
</div>
<p>The PEP also proposes that all <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statements be absolute imports,
with a leading <code class="docutils literal notranslate"><span class="pre">.</span></code> character to indicate a relative import. This part of the
PEP was not implemented for Python 2.4, but was completed for Python 2.5.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-37"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a> - 导入:多行和绝对/相对导入</dt><dd><p>由 Aahz 撰写,多行导入由 Dima Dorfman 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-331-locale-independent-float-string-conversions">
<h2>PEP 331: Locale-Independent Float/String Conversions<a class="headerlink" href="#pep-331-locale-independent-float-string-conversions" title="永久链接至标题"></a></h2>
<p>The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> modules lets Python software select various conversions and
display conventions that are localized to a particular country or language.
However, the module was careful to not change the numeric locale because various
functions in Python's implementation required that the numeric locale remain set
to the <code class="docutils literal notranslate"><span class="pre">'C'</span></code> locale. Often this was because the code was using the C
library's <code class="xref c c-func docutils literal notranslate"><span class="pre">atof()</span></code> function.</p>
<p>Not setting the numeric locale caused trouble for extensions that used third-party
C libraries, however, because they wouldn't have the correct locale set.
The motivating example was GTK+, whose user interface widgets weren't displaying
numbers in the current locale.</p>
<p>The solution described in the PEP is to add three new functions to the Python
API that perform ASCII-only conversions, ignoring the locale setting:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">PyOS_ascii_strtod(str,</span> <span class="pre">ptr)</span></code> and <code class="docutils literal notranslate"><span class="pre">PyOS_ascii_atof(str,</span> <span class="pre">ptr)</span></code>
both convert a string to a C <code class="xref c c-type docutils literal notranslate"><span class="pre">double</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PyOS_ascii_formatd(buffer,</span> <span class="pre">buf_len,</span> <span class="pre">format,</span> <span class="pre">d)</span></code> converts a
<code class="xref c c-type docutils literal notranslate"><span class="pre">double</span></code> to an ASCII string.</p></li>
</ul>
<p>The code for these functions came from the GLib library
(<a class="reference external" href="https://developer.gnome.org/glib/stable/">https://developer.gnome.org/glib/stable/</a>), whose developers kindly
relicensed the relevant functions and donated them to the Python Software
Foundation. The <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module can now change the numeric locale,
letting extensions such as GTK+ produce the correct results.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0331"><strong>PEP 331</strong></a> - Locale-Independent Float/String Conversions</dt><dd><p>由Christian R. Reis撰写由 Gustavo Carneiro 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="other-language-changes">
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题"></a></h2>
<p>以下是 Python 2.4 针对核心 Python 语言的所有改变。</p>
<ul>
<li><p>增加了用于函数和方法的装饰器 (<span class="target" id="index-38"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0318"><strong>PEP 318</strong></a>)。</p></li>
<li><p>Built-in <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-func docutils literal notranslate"><span class="pre">frozenset()</span></code></a> types were added (<span class="target" id="index-13"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0218"><strong>PEP 218</strong></a>).
Other new built-ins include the <code class="docutils literal notranslate"><span class="pre">reversed(seq)</span></code> function (<span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0322"><strong>PEP 322</strong></a>).</p></li>
<li><p>Generator expressions were added (<span class="target" id="index-15"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0289"><strong>PEP 289</strong></a>).</p></li>
<li><p>Certain numeric expressions no longer return values restricted to 32 or 64
bits (<span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0237"><strong>PEP 237</strong></a>).</p></li>
<li><p>You can now put parentheses around the list of names in a <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span>
<span class="pre">names</span></code> statement (<span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a>).</p></li>
<li><p>The <a class="reference internal" href="../library/stdtypes.html#dict.update" title="dict.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.update()</span></code></a> method now accepts the same argument forms as the
<a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> constructor. This includes any mapping, any iterable of key/value
pairs, and keyword arguments. (Contributed by Raymond Hettinger.)</p></li>
<li><p>The string methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">ljust()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rjust()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">center()</span></code> now take
an optional argument for specifying a fill character other than a space.
(Contributed by Raymond Hettinger.)</p></li>
<li><p>Strings also gained an <code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code> method that works like the <code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code>
method but splits from the end of the string. (Contributed by Sean
Reifschneider.)</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;www.python.org&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">[&#39;www&#39;, &#39;python.org&#39;]</span>
<span class="go">&#39;www.python.org&#39;.rsplit(&#39;.&#39;, 1)</span>
<span class="go">[&#39;www.python&#39;, &#39;org&#39;]</span>
</pre></div>
</div>
</li>
<li><p>Three keyword parameters, <em>cmp</em>, <em>key</em>, and <em>reverse</em>, were added to the
<code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> method of lists. These parameters make some common usages of
<code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> simpler. All of these parameters are optional.</p>
<p>For the <em>cmp</em> parameter, the value should be a comparison function that takes
two parameters and returns -1, 0, or +1 depending on how the parameters compare.
This function will then be used to sort the list. Previously this was the only
parameter that could be provided to <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code>.</p>
<p><em>key</em> should be a single-parameter function that takes a list element and
returns a comparison key for the element. The list is then sorted using the
comparison keys. The following example sorts a list case-insensitively:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;D&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span> <span class="c1"># Case-sensitive sort</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">[&#39;A&#39;, &#39;D&#39;, &#39;b&#39;, &#39;c&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Using &#39;key&#39; parameter to sort list</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Old-fashioned way</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">cmp</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">:</span> <span class="n">cmp</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">y</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
</pre></div>
</div>
<p>The last example, which uses the <em>cmp</em> parameter, is the old way to perform a
case-insensitive sort. It works but is slower than using a <em>key</em> parameter.
Using <em>key</em> calls <code class="xref py py-meth docutils literal notranslate"><span class="pre">lower()</span></code> method once for each element in the list while
using <em>cmp</em> will call it twice for each comparison, so using <em>key</em> saves on
invocations of the <code class="xref py py-meth docutils literal notranslate"><span class="pre">lower()</span></code> method.</p>
<p>For simple key functions and comparison functions, it is often possible to avoid
a <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> expression by using an unbound method instead. For example,
the above case-insensitive sort is best written as:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="nb">str</span><span class="o">.</span><span class="n">lower</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span>
<span class="go">[&#39;A&#39;, &#39;b&#39;, &#39;c&#39;, &#39;D&#39;]</span>
</pre></div>
</div>
<p>Finally, the <em>reverse</em> parameter takes a Boolean value. If the value is true,
the list will be sorted into reverse order. Instead of <code class="docutils literal notranslate"><span class="pre">L.sort();</span>
<span class="pre">L.reverse()</span></code>, you can now write <code class="docutils literal notranslate"><span class="pre">L.sort(reverse=True)</span></code>.</p>
<p>The results of sorting are now guaranteed to be stable. This means that two
entries with equal keys will be returned in the same order as they were input.
For example, you can sort a list of people by name, and then sort the list by
age, resulting in a list sorted by age where people with the same age are in
name-sorted order.</p>
<p>(All changes to <code class="xref py py-meth docutils literal notranslate"><span class="pre">sort()</span></code> contributed by Raymond Hettinger.)</p>
</li>
<li><p>There is a new built-in function <code class="docutils literal notranslate"><span class="pre">sorted(iterable)</span></code> that works like the
in-place <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> method but can be used in expressions. The
differences are:</p></li>
<li><p>输入可以是任意可迭代对象;</p></li>
<li><p>a newly formed copy is sorted, leaving the original intact; and</p></li>
<li><p>the expression returns the new sorted copy</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">9</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">10</span><span class="o">+</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">L</span><span class="p">)]</span> <span class="c1"># usable in a list comprehension</span>
<span class="go">[11, 12, 13, 14, 15, 16, 17, 18, 19]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="c1"># original is left unchanged</span>
<span class="go">[9,7,8,3,2,4,1,6,5]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="s1">&#39;Monty Python&#39;</span><span class="p">)</span> <span class="c1"># any iterable may be an input</span>
<span class="go">[&#39; &#39;, &#39;M&#39;, &#39;P&#39;, &#39;h&#39;, &#39;n&#39;, &#39;n&#39;, &#39;o&#39;, &#39;o&#39;, &#39;t&#39;, &#39;t&#39;, &#39;y&#39;, &#39;y&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># List the contents of a dict sorted by key values</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">colormap</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">red</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">blue</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">green</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">black</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">yellow</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">colormap</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span>
<span class="gp">...</span>
<span class="go">black 4</span>
<span class="go">blue 2</span>
<span class="go">green 3</span>
<span class="go">red 1</span>
<span class="go">yellow 5</span>
</pre></div>
</div>
<p>(由 Raymond Hettinger 贡献。)</p>
</li>
<li><p>Integer operations will no longer trigger an <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code>. The
<code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code> warning will disappear in Python 2.5.</p></li>
<li><p>The interpreter gained a new switch, <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a>, that takes a name, searches
for the corresponding module on <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, and runs the module as a script.
For example, you can now run the Python profiler with <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">profile</span></code>.
(Contributed by Nick Coghlan.)</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">eval(expr,</span> <span class="pre">globals,</span> <span class="pre">locals)</span></code> and <code class="docutils literal notranslate"><span class="pre">execfile(filename,</span> <span class="pre">globals,</span>
<span class="pre">locals)</span></code> functions and the <code class="docutils literal notranslate"><span class="pre">exec</span></code> statement now accept any mapping type
for the <em>locals</em> parameter. Previously this had to be a regular Python
dictionary. (Contributed by Raymond Hettinger.)</p></li>
<li><p>The <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> built-in function and <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code> now return an
empty list if called with no arguments. Previously they raised a
<a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception. This makes them more suitable for use with variable
length argument lists:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">transpose</span><span class="p">(</span><span class="n">array</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">array</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">transpose</span><span class="p">([(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">)])</span>
<span class="go">[(1, 4), (2, 5), (3, 6)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">transpose</span><span class="p">([])</span>
<span class="go">[]</span>
</pre></div>
</div>
<p>(由 Raymond Hettinger 贡献。)</p>
</li>
<li><p>Encountering a failure while importing a module no longer leaves a partially-initialized
module object in <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>. The incomplete module object left
behind would fool further imports of the same module into succeeding, leading to
confusing errors. (Fixed by Tim Peters.)</p></li>
<li><p><a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 现在是一个常量;将一个新值绑定到名称 <code class="docutils literal notranslate"><span class="pre">None</span></code> 的代码现在会造成语法错误。 (由 Raymond Hettinger 贡献。)</p></li>
</ul>
<section id="optimizations">
<h3>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>The inner loops for list and tuple slicing were optimized and now run about
one-third faster. The inner loops for dictionaries were also optimized,
resulting in performance boosts for <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code>,
<code class="xref py py-meth docutils literal notranslate"><span class="pre">iterkeys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">itervalues()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">iteritems()</span></code>. (Contributed by
Raymond Hettinger.)</p></li>
<li><p>The machinery for growing and shrinking lists was optimized for speed and for
space efficiency. Appending and popping from lists now runs faster due to more
efficient code paths and less frequent use of the underlying system
<code class="xref c c-func docutils literal notranslate"><span class="pre">realloc()</span></code>. List comprehensions also benefit. <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.extend()</span></code> was
also optimized and no longer converts its argument into a temporary list before
extending the base list. (Contributed by Raymond Hettinger.)</p></li>
<li><p><a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">tuple()</span></code></a>, <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>, <a class="reference internal" href="../library/functions.html#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a>, and <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> now
run several times faster with non-sequence arguments that supply a
<a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a> method. (Contributed by Raymond Hettinger.)</p></li>
<li><p>The methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.__getitem__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.__getitem__()</span></code>, and
<code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.__contains__()</span></code> are now implemented as <code class="xref py py-class docutils literal notranslate"><span class="pre">method_descriptor</span></code>
objects rather than <code class="xref py py-class docutils literal notranslate"><span class="pre">wrapper_descriptor</span></code> objects. This form of access
doubles their performance and makes them more suitable for use as arguments to
functionals: <code class="docutils literal notranslate"><span class="pre">map(mydict.__getitem__,</span> <span class="pre">keylist)</span></code>. (Contributed by Raymond
Hettinger.)</p></li>
<li><p>Added a new opcode, <code class="docutils literal notranslate"><span class="pre">LIST_APPEND</span></code>, that simplifies the generated bytecode
for list comprehensions and speeds them up by about a third. (Contributed by
Raymond Hettinger.)</p></li>
<li><p>The peephole bytecode optimizer has been improved to produce shorter, faster
bytecode; remarkably, the resulting bytecode is more readable. (Enhanced by
Raymond Hettinger.)</p></li>
<li><p>String concatenations in statements of the form <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">=</span> <span class="pre">s</span> <span class="pre">+</span> <span class="pre">&quot;abc&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">+=</span>
<span class="pre">&quot;abc&quot;</span></code> are now performed more efficiently in certain circumstances. This
optimization won't be present in other Python implementations such as Jython, so
you shouldn't rely on it; using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> method of strings is still
recommended when you want to efficiently glue a large number of strings
together. (Contributed by Armin Rigo.)</p></li>
</ul>
<p>The net result of the 2.4 optimizations is that Python 2.4 runs the pystone
benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2.
(pystone is not a particularly good benchmark, but it's the most commonly used
measurement of Python's performance. Your own applications may show greater or
smaller benefits from Python 2.4.)</p>
</section>
</section>
<section id="new-improved-and-deprecated-modules">
<h2>新增,改进和弃用的模块<a class="headerlink" href="#new-improved-and-deprecated-modules" title="永久链接至标题"></a></h2>
<p>As usual, Python's standard library received a number of enhancements and bug
fixes. Here's a partial list of the most notable changes, sorted alphabetically
by module name. Consult the <code class="file docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more
complete list of changes, or look through the CVS logs for all the details.</p>
<ul>
<li><p>The <a class="reference internal" href="../library/asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncore</span></code></a> module's <code class="xref py py-func docutils literal notranslate"><span class="pre">loop()</span></code> function now has a <em>count</em> parameter
that lets you perform a limited number of passes through the polling loop. The
default is still to loop forever.</p></li>
<li><p>The <a class="reference internal" href="../library/base64.html#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> module now has more complete <span class="target" id="index-18"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> support for Base64,
Base32, and Base16 encoding and decoding, including optional case folding and
optional alternative alphabets. (Contributed by Barry Warsaw.)</p></li>
<li><p>The <a class="reference internal" href="../library/bisect.html#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code></a> module now has an underlying C implementation for improved
performance. (Contributed by Dmitry Vasiliev.)</p></li>
<li><p>由 Hye-Shik Chang 维护的东亚编解码器的 CJKCodecs 集合已集成到 2.4 中。新的编码为:</p></li>
<li><p>汉语(大陆): gb2312, gbk, gb18030, big5hkscs, hz</p></li>
<li><p>汉语(台湾): big5, cp950</p></li>
<li><dl class="simple">
<dt>日语: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,</dt><dd><p>iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004,
shift-jis, shift-jisx0213, shift-jis-2004</p>
</dd>
</dl>
</li>
<li><p>韩语: cp949, euc-kr, johab, iso-2022-kr</p></li>
<li><p>添加了其他一些新的编码HP Roman8, ISO_8859-11, ISO_8859-16, PCTP-154 和 TIS-620</p></li>
<li><p>The UTF-8 and UTF-16 codecs now cope better with receiving partial input.
Previously the <code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code> class would try to read more data, making
it impossible to resume decoding from the stream. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> method will
now return as much data as it can and future calls will resume decoding where
previous ones left off. (Implemented by Walter Dörwald.)</p></li>
<li><p>There is a new <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module for various specialized collection
datatypes. Currently it contains just one type, <code class="xref py py-class docutils literal notranslate"><span class="pre">deque</span></code>, a double-ended
queue that supports efficiently adding and removing elements from either
end:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s1">&#39;ghi&#39;</span><span class="p">)</span> <span class="c1"># make a new deque with three items</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;j&#39;</span><span class="p">)</span> <span class="c1"># add a new entry to the right side</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">appendleft</span><span class="p">(</span><span class="s1">&#39;f&#39;</span><span class="p">)</span> <span class="c1"># add a new entry to the left side</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="c1"># show the representation of the deque</span>
<span class="go">deque([&#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="c1"># return and remove the rightmost item</span>
<span class="go">&#39;j&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c1"># return and remove the leftmost item</span>
<span class="go">&#39;f&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="c1"># list the contents of the deque</span>
<span class="go">[&#39;g&#39;, &#39;h&#39;, &#39;i&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;h&#39;</span> <span class="ow">in</span> <span class="n">d</span> <span class="c1"># search the deque</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Several modules, such as the <code class="xref py py-mod docutils literal notranslate"><span class="pre">Queue</span></code> and <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> modules, now take
advantage of <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> for improved performance. (Contributed
by Raymond Hettinger.)</p>
</li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code> classes have been enhanced slightly. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code>
method now returns a list of the files that were successfully parsed, and the
<a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method raises <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if passed a <em>value</em> argument that
isn't a string. (Contributed by John Belmonte and David Goodger.)</p></li>
<li><p>The <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module now supports the ncurses extension
<code class="xref py py-func docutils literal notranslate"><span class="pre">use_default_colors()</span></code>. On platforms where the terminal supports
transparency, this makes it possible to use a transparent background.
(Contributed by Jörg Lehmann.)</p></li>
<li><p>The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module now includes an <code class="xref py py-class docutils literal notranslate"><span class="pre">HtmlDiff</span></code> class that creates
an HTML table showing a side by side comparison of two versions of a text.
(Contributed by Dan Gass.)</p></li>
<li><p>The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package was updated to version 3.0, which dropped various
deprecated APIs and removes support for Python versions earlier than 2.3. The
3.0 version of the package uses a new incremental parser for MIME messages,
available in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">email.FeedParser</span></code> module. The new parser doesn't require
reading the entire message into memory, and doesn't raise exceptions if a
message is malformed; instead it records any problems in the <code class="xref py py-attr docutils literal notranslate"><span class="pre">defect</span></code>
attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas
Wouters, and others.)</p></li>
<li><p>The <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code></a> module has been converted to C. The resulting tenfold
improvement in speed makes the module suitable for handling high volumes of
data. In addition, the module has two new functions <code class="xref py py-func docutils literal notranslate"><span class="pre">nlargest()</span></code> and
<code class="xref py py-func docutils literal notranslate"><span class="pre">nsmallest()</span></code> that use heaps to find the N largest or smallest values in a
dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> module now contains constants for HTTP status codes defined
in various HTTP-related RFC documents. Constants have names such as
<code class="xref py py-const docutils literal notranslate"><span class="pre">OK</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">CREATED</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">CONTINUE</span></code>, and
<code class="xref py py-const docutils literal notranslate"><span class="pre">MOVED_PERMANENTLY</span></code>; use pydoc to get a full list. (Contributed by
Andrew Eland.)</p></li>
<li><p>The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> module now supports IMAP's THREAD command (contributed by
Yves Dionne) and new <code class="xref py py-meth docutils literal notranslate"><span class="pre">deleteacl()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">myrights()</span></code> methods (contributed
by Arnaud Mazin).</p></li>
<li><p>The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module gained a <code class="docutils literal notranslate"><span class="pre">groupby(iterable[,</span> <span class="pre">*func*])</span></code>
function. <em>iterable</em> is something that can be iterated over to return a stream
of elements, and the optional <em>func</em> parameter is a function that takes an
element and returns a key value; if omitted, the key is simply the element
itself. <code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> then groups the elements into subsequences which have
matching values of the key, and returns a series of 2-tuples containing the key
value and an iterator over the subsequence.</p>
<p>Here's an example to make this clearer. The <em>key</em> function simply returns
whether a number is even or odd, so the result of <code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is to return
consecutive runs of odd or even numbers.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">itertools</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">14</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">key_val</span><span class="p">,</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">key_val</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0 [2, 4, 6]</span>
<span class="go">1 [7]</span>
<span class="go">0 [8]</span>
<span class="go">1 [9, 11]</span>
<span class="go">0 [12, 14]</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p><code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is typically used with sorted input. The logic for
<code class="xref py py-func docutils literal notranslate"><span class="pre">groupby()</span></code> is similar to the Unix <code class="docutils literal notranslate"><span class="pre">uniq</span></code> filter which makes it handy for
eliminating, counting, or identifying duplicate elements:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">word</span> <span class="o">=</span> <span class="s1">&#39;abracadabra&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">letters</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="c1"># Turn string into a sorted list of letters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">letters</span>
<span class="go">[&#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;b&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;r&#39;, &#39;r&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">):</span>
<span class="gp">... </span> <span class="nb">print</span> <span class="n">k</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">a [&#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;, &#39;a&#39;]</span>
<span class="go">b [&#39;b&#39;, &#39;b&#39;]</span>
<span class="go">c [&#39;c&#39;]</span>
<span class="go">d [&#39;d&#39;]</span>
<span class="go">r [&#39;r&#39;, &#39;r&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># List unique letters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">)]</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;r&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Count letter occurrences</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">k</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">)))</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">letters</span><span class="p">)]</span>
<span class="go">[(&#39;a&#39;, 5), (&#39;b&#39;, 2), (&#39;c&#39;, 1), (&#39;d&#39;, 1), (&#39;r&#39;, 2)]</span>
</pre></div>
</div>
<p>(由 Hye-Shik Chang 贡献。)</p>
</li>
<li><p><a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> also gained a function named <code class="docutils literal notranslate"><span class="pre">tee(iterator,</span> <span class="pre">N)</span></code> that
returns <em>N</em> independent iterators that replicate <em>iterator</em>. If <em>N</em> is omitted,
the default is 2.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span><span class="p">,</span> <span class="n">i2</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">tee</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i1</span><span class="p">,</span><span class="n">i2</span>
<span class="go">(&lt;itertools.tee object at 0x402c2080&gt;, &lt;itertools.tee object at 0x402c2090&gt;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">i1</span><span class="p">)</span> <span class="c1"># Run the first iterator to exhaustion</span>
<span class="go">[1, 2, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">i2</span><span class="p">)</span> <span class="c1"># Run the second iterator to exhaustion</span>
<span class="go">[1, 2, 3]</span>
</pre></div>
</div>
<p>Note that <code class="xref py py-func docutils literal notranslate"><span class="pre">tee()</span></code> has to keep copies of the values returned by the
iterator; in the worst case, it may need to keep all of them. This should
therefore be used carefully if the leading iterator can run far ahead of the
trailing iterator in a long stream of inputs. If the separation is large, then
you might as well use <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a> instead. When the iterators track closely
with one another, <code class="xref py py-func docutils literal notranslate"><span class="pre">tee()</span></code> is ideal. Possible applications include
bookmarking, windowing, or lookahead iterators. (Contributed by Raymond
Hettinger.)</p>
</li>
<li><p>A number of functions were added to the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> module, such as
<code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code> to specify a particular encoding and a family of
<code class="xref py py-func docutils literal notranslate"><span class="pre">l*gettext()</span></code> functions that return messages in the chosen encoding.
(Contributed by Gustavo Niemeyer.)</p></li>
<li><p>Some keyword arguments were added to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> package's
<code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code> function to simplify log configuration. The default
behavior is to log messages to standard error, but various keyword arguments can
be specified to log to a particular file, change the logging format, or set the
logging level. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s1">&#39;/var/log/application.log&#39;</span><span class="p">,</span>
<span class="n">level</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="c1"># Log all messages</span>
<span class="nb">format</span><span class="o">=</span><span class="s1">&#39;%(levelname):%(process):%(thread):%(message)&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Other additions to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> package include a <code class="docutils literal notranslate"><span class="pre">log(level,</span> <span class="pre">msg)</span></code>
convenience method, as well as a <code class="xref py py-class docutils literal notranslate"><span class="pre">TimedRotatingFileHandler</span></code> class that
rotates its log files at a timed interval. The module already had
<code class="xref py py-class docutils literal notranslate"><span class="pre">RotatingFileHandler</span></code>, which rotated logs once the file exceeded a
certain size. Both classes derive from a new <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseRotatingHandler</span></code> class
that can be used to implement other rotating handlers.</p>
<p>(更改由 Vinay Sajip 实现。)</p>
</li>
<li><p>The <a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a> module now shares interned strings on unpacking a data
structure. This may shrink the size of certain pickle strings, but the primary
effect is to make <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> files significantly smaller. (Contributed by
Martin von Löwis.)</p></li>
<li><p>The <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a> module's <code class="xref py py-class docutils literal notranslate"><span class="pre">NNTP</span></code> class gained <code class="xref py py-meth docutils literal notranslate"><span class="pre">description()</span></code> and
<code class="xref py py-meth docutils literal notranslate"><span class="pre">descriptions()</span></code> methods to retrieve newsgroup descriptions for a single
group or for a range of groups. (Contributed by Jürgen A. Erhard.)</p></li>
<li><p>Two new functions were added to the <a class="reference internal" href="../library/operator.html#module-operator" title="operator: Functions corresponding to the standard operators."><code class="xref py py-mod docutils literal notranslate"><span class="pre">operator</span></code></a> module,
<code class="docutils literal notranslate"><span class="pre">attrgetter(attr)</span></code> and <code class="docutils literal notranslate"><span class="pre">itemgetter(index)</span></code>. Both functions return
callables that take a single argument and return the corresponding attribute or
item; these callables make excellent data extractors when used with <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>
or <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">L</span><span class="p">)</span>
<span class="go">[&#39;c&#39;, &#39;d&#39;, &#39;a&#39;, &#39;b&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">L</span><span class="p">)</span>
<span class="go">[2, 1, 4, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="c1"># Sort list by second tuple item</span>
<span class="go">[(&#39;d&#39;, 1), (&#39;c&#39;, 2), (&#39;b&#39;, 3), (&#39;a&#39;, 4)]</span>
</pre></div>
</div>
<p>(由 Raymond Hettinger 贡献。)</p>
</li>
<li><p>The <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> module was updated in various ways. The module now passes
its messages through <a class="reference internal" href="../library/gettext.html#gettext.gettext" title="gettext.gettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettext.gettext()</span></code></a>, making it possible to
internationalize Optik's help and error messages. Help messages for options can
now include the string <code class="docutils literal notranslate"><span class="pre">'%default'</span></code>, which will be replaced by the option's
default value. (Contributed by Greg Ward.)</p></li>
<li><p>The long-term plan is to deprecate the <code class="xref py py-mod docutils literal notranslate"><span class="pre">rfc822</span></code> module in some future
Python release in favor of the <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package. To this end, the
<code class="xref py py-func docutils literal notranslate"><span class="pre">email.Utils.formatdate()</span></code> function has been changed to make it usable as a
replacement for <code class="xref py py-func docutils literal notranslate"><span class="pre">rfc822.formatdate()</span></code>. You may want to write new e-mail
processing code with this in mind. (Change implemented by Anthony Baxter.)</p></li>
<li><p>A new <code class="docutils literal notranslate"><span class="pre">urandom(n)</span></code> function was added to the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module, returning
a string containing <em>n</em> bytes of random data. This function provides access to
platform-specific sources of randomness such as <code class="file docutils literal notranslate"><span class="pre">/dev/urandom</span></code> on Linux or
the Windows CryptoAPI. (Contributed by Trevor Perrin.)</p></li>
<li><p>Another new function: <code class="docutils literal notranslate"><span class="pre">os.path.lexists(path)</span></code> returns true if the file
specified by <em>path</em> exists, whether or not it's a symbolic link. This differs
from the existing <code class="docutils literal notranslate"><span class="pre">os.path.exists(path)</span></code> function, which returns false if
<em>path</em> is a symlink that points to a destination that doesn't exist.
(Contributed by Beni Cherniavsky.)</p></li>
<li><p><a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> 之下 <a class="reference internal" href="../library/posix.html#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">posix</span></code></a> 模块中新增了一个 <code class="xref py py-func docutils literal notranslate"><span class="pre">getsid()</span></code> 函数。 (由 J. Raynor 贡献。)</p></li>
<li><p><a class="reference internal" href="../library/poplib.html#module-poplib" title="poplib: POP3 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">poplib</span></code></a> 模块现在已支持 SSL 上的 POP。 (由 Hector Urtubia 贡献。)</p></li>
<li><p>现在 <a class="reference internal" href="../library/profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile</span></code></a> 模块将可对 C 扩展函数执行性能分析。 (由 Nick Bastin 贡献。)</p></li>
<li><p>The <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> module has a new method called <code class="docutils literal notranslate"><span class="pre">getrandbits(N)</span></code> that
returns a long integer <em>N</em> bits in length. The existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">randrange()</span></code>
method now uses <code class="xref py py-meth docutils literal notranslate"><span class="pre">getrandbits()</span></code> where appropriate, making generation of
arbitrarily large random numbers more efficient. (Contributed by Raymond
Hettinger.)</p></li>
<li><p>The regular expression language accepted by the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module was extended
with simple conditional expressions, written as <code class="docutils literal notranslate"><span class="pre">(?(group)A|B)</span></code>. <em>group</em> is
either a numeric group ID or a group name defined with <code class="docutils literal notranslate"><span class="pre">(?P&lt;group&gt;...)</span></code>
earlier in the expression. If the specified group matched, the regular
expression pattern <em>A</em> will be tested against the string; if the group didn't
match, the pattern <em>B</em> will be used instead. (Contributed by Gustavo Niemeyer.)</p></li>
<li><p>The <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module is also no longer recursive, thanks to a massive amount
of work by Gustavo Niemeyer. In a recursive regular expression engine, certain
patterns result in a large amount of C stack space being consumed, and it was
possible to overflow the stack. For example, if you matched a 30000-byte string
of <code class="docutils literal notranslate"><span class="pre">a</span></code> characters against the expression <code class="docutils literal notranslate"><span class="pre">(a|b)+</span></code>, one stack frame was
consumed per character. Python 2.3 tried to check for stack overflow and raise
a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> exception, but certain patterns could sidestep the
checking and if you were unlucky Python could segfault. Python 2.4's regular
expression engine can match this pattern without problems.</p></li>
<li><p>The <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a> module now performs tighter error-checking on the parameters
to the <a class="reference internal" href="../library/signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a> function. For example, you can't set a handler on
the <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGKILL</span></code> signal; previous versions of Python would quietly accept
this, but 2.4 will raise a <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> exception.</p></li>
<li><p>Two new functions were added to the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module. <code class="xref py py-func docutils literal notranslate"><span class="pre">socketpair()</span></code>
returns a pair of connected sockets and <code class="docutils literal notranslate"><span class="pre">getservbyport(port)</span></code> looks up the
service name for a given port number. (Contributed by Dave Cole and Barry
Warsaw.)</p></li>
<li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code> function has been deprecated. Code should be using
the existing <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module, which correctly handles calling multiple exit
functions. Eventually <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code> will become a purely internal
interface, accessed only by <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a>.</p></li>
<li><p>现在 <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> 模块默认将生成 GNU 格式的 tar 文件。 (由 Lars Gustäbel 贡献。)</p></li>
<li><p>The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module now has an elegantly simple way to support
thread-local data. The module contains a <code class="xref py py-class docutils literal notranslate"><span class="pre">local</span></code> class whose attribute
values are local to different threads.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span>
<span class="n">data</span><span class="o">.</span><span class="n">number</span> <span class="o">=</span> <span class="mi">42</span>
<span class="n">data</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
</pre></div>
</div>
<p>Other threads can assign and retrieve their own values for the <code class="xref py py-attr docutils literal notranslate"><span class="pre">number</span></code>
and <code class="xref py py-attr docutils literal notranslate"><span class="pre">url</span></code> attributes. You can subclass <code class="xref py py-class docutils literal notranslate"><span class="pre">local</span></code> to initialize
attributes or to add methods. (Contributed by Jim Fulton.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeit</span></code></a> module now automatically disables periodic garbage
collection during the timing loop. This change makes consecutive timings more
comparable. (Contributed by Raymond Hettinger.)</p></li>
<li><p>The <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module now supports a wider variety of objects including
Python functions, class instances, sets, frozensets, deques, arrays, files,
sockets, and regular expression pattern objects. (Contributed by Raymond
Hettinger.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code> module now supports a multi-call extension for
transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by
Brian Quinlan.)</p></li>
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">mpz</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">rotor</span></code><code class="xref py py-mod docutils literal notranslate"><span class="pre">xreadlines</span></code> 模块已被移除。</p></li>
</ul>
<section id="cookielib">
<h3>cookielib<a class="headerlink" href="#cookielib" title="永久链接至标题"></a></h3>
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code> library supports client-side handling for HTTP cookies,
mirroring the <code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code> module's server-side cookie support. Cookies are
stored in cookie jars; the library transparently stores cookies offered by the
web server in the cookie jar, and fetches the cookie from the jar when
connecting to the server. As in web browsers, policy objects control whether
cookies are accepted or not.</p>
<p>In order to store cookies across sessions, two implementations of cookie jars
are provided: one that stores cookies in the Netscape format so applications can
use the Mozilla or Lynx cookie files, and one that stores cookies in the same
format as the Perl libwww library.</p>
<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code> has been changed to interact with <code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code>:
<code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPCookieProcessor</span></code> manages a cookie jar that is used when accessing
URLs.</p>
<p>该模块由 John J. Lee 贡献。</p>
</section>
<section id="doctest">
<h3>doctest<a class="headerlink" href="#doctest" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module underwent considerable refactoring thanks to Edward
Loper and Tim Peters. Testing can still be as simple as running
<a class="reference internal" href="../library/doctest.html#doctest.testmod" title="doctest.testmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">doctest.testmod()</span></code></a>, but the refactorings allow customizing the module's
operation in various ways</p>
<p>The new <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestFinder</span></code> class extracts the tests from a given object's
docstrings:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; f(2,2)</span>
<span class="sd">4</span>
<span class="sd">&gt;&gt;&gt; f(3,2)</span>
<span class="sd">6</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span>
<span class="n">finder</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">DocTestFinder</span><span class="p">()</span>
<span class="c1"># Get list of DocTest instances</span>
<span class="n">tests</span> <span class="o">=</span> <span class="n">finder</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>The new <code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code> class then runs individual tests and can produce
a summary of the results:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">runner</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">DocTestRunner</span><span class="p">()</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">tests</span><span class="p">:</span>
<span class="n">tried</span><span class="p">,</span> <span class="n">failed</span> <span class="o">=</span> <span class="n">runner</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="n">runner</span><span class="o">.</span><span class="n">summarize</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>The above example produces the following output:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">items</span> <span class="n">passed</span> <span class="nb">all</span> <span class="n">tests</span><span class="p">:</span>
<span class="mi">2</span> <span class="n">tests</span> <span class="ow">in</span> <span class="n">f</span>
<span class="mi">2</span> <span class="n">tests</span> <span class="ow">in</span> <span class="mi">1</span> <span class="n">items</span><span class="o">.</span>
<span class="mi">2</span> <span class="n">passed</span> <span class="ow">and</span> <span class="mi">0</span> <span class="n">failed</span><span class="o">.</span>
<span class="n">Test</span> <span class="n">passed</span><span class="o">.</span>
</pre></div>
</div>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">DocTestRunner</span></code> uses an instance of the <code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code> class to
compare the expected output with the actual output. This class takes a number
of different flags that customize its behaviour; ambitious users can also write
a completely new subclass of <code class="xref py py-class docutils literal notranslate"><span class="pre">OutputChecker</span></code>.</p>
<p>The default output checker provides a number of handy features. For example,
with the <a class="reference internal" href="../library/doctest.html#doctest.ELLIPSIS" title="doctest.ELLIPSIS"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.ELLIPSIS</span></code></a> option flag, an ellipsis (<code class="docutils literal notranslate"><span class="pre">...</span></code>) in the
expected output matches any substring, making it easier to accommodate outputs
that vary in minor ways:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">o</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; o(1)</span>
<span class="sd">&lt;__main__.C instance at 0x...&gt;</span>
<span class="sd">&gt;&gt;&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>Another special string, <code class="docutils literal notranslate"><span class="pre">&lt;BLANKLINE&gt;</span></code>, matches a blank line:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">p</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; p(1)</span>
<span class="sd">&lt;BLANKLINE&gt;</span>
<span class="sd">&gt;&gt;&gt;</span>
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>Another new capability is producing a diff-style display of the output by
specifying the <a class="reference internal" href="../library/doctest.html#doctest.REPORT_UDIFF" title="doctest.REPORT_UDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_UDIFF</span></code></a> (unified diffs),
<a class="reference internal" href="../library/doctest.html#doctest.REPORT_CDIFF" title="doctest.REPORT_CDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_CDIFF</span></code></a> (context diffs), or <a class="reference internal" href="../library/doctest.html#doctest.REPORT_NDIFF" title="doctest.REPORT_NDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_NDIFF</span></code></a>
(delta-style) option flags. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">g</span> <span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;&gt;&gt;&gt; g(4)</span>
<span class="sd">here</span>
<span class="sd">is</span>
<span class="sd">a</span>
<span class="sd">lengthy</span>
<span class="sd">&gt;&gt;&gt;&quot;&quot;&quot;</span>
<span class="n">L</span> <span class="o">=</span> <span class="s1">&#39;here is a rather lengthy list of words&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
<span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">L</span><span class="p">[:</span><span class="n">n</span><span class="p">]:</span>
<span class="nb">print</span> <span class="n">word</span>
</pre></div>
</div>
<p>Running the above function's tests with <a class="reference internal" href="../library/doctest.html#doctest.REPORT_UDIFF" title="doctest.REPORT_UDIFF"><code class="xref py py-const docutils literal notranslate"><span class="pre">doctest.REPORT_UDIFF</span></code></a> specified,
you get the following output:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>**********************************************************************
File &quot;t.py&quot;, line 15, in g
Failed example:
g(4)
Differences (unified diff with -expected +actual):
@@ -2,3 +2,3 @@
is
a
-lengthy
+rather
**********************************************************************
</pre></div>
</div>
</section>
</section>
<section id="build-and-c-api-changes">
<h2>构建和 C API 的改变<a class="headerlink" href="#build-and-c-api-changes" title="永久链接至标题"></a></h2>
<p>对于 Python 构建过程和 C API 的一些修改:</p>
<ul class="simple">
<li><p>Three new convenience macros were added for common return values from
extension functions: <a class="reference internal" href="../c-api/none.html#c.Py_RETURN_NONE" title="Py_RETURN_NONE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_NONE</span></code></a>, <a class="reference internal" href="../c-api/bool.html#c.Py_RETURN_TRUE" title="Py_RETURN_TRUE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_TRUE</span></code></a>, and
<a class="reference internal" href="../c-api/bool.html#c.Py_RETURN_FALSE" title="Py_RETURN_FALSE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_FALSE</span></code></a>. (Contributed by Brett Cannon.)</p></li>
<li><p>Another new macro, <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_CLEAR(obj)</span></code>, decreases the reference count of
<em>obj</em> and sets <em>obj</em> to the null pointer. (Contributed by Jim Fulton.)</p></li>
<li><p>A new function, <code class="docutils literal notranslate"><span class="pre">PyTuple_Pack(N,</span> <span class="pre">obj1,</span> <span class="pre">obj2,</span> <span class="pre">...,</span> <span class="pre">objN)</span></code>, constructs
tuples from a variable length argument list of Python objects. (Contributed by
Raymond Hettinger.)</p></li>
<li><p>A new function, <code class="docutils literal notranslate"><span class="pre">PyDict_Contains(d,</span> <span class="pre">k)</span></code>, implements fast dictionary
lookups without masking exceptions raised during the look-up process.
(Contributed by Raymond Hettinger.)</p></li>
<li><p>The <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_IS_NAN(X)</span></code> macro returns 1 if its float or double argument
<em>X</em> is a NaN. (Contributed by Tim Peters.)</p></li>
<li><p>C code can avoid unnecessary locking by using the new
<a class="reference internal" href="../c-api/init.html#c.PyEval_ThreadsInitialized" title="PyEval_ThreadsInitialized"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ThreadsInitialized()</span></code></a> function to tell if any thread operations
have been performed. If this function returns false, no lock operations are
needed. (Contributed by Nick Coghlan.)</p></li>
<li><p>A new function, <a class="reference internal" href="../c-api/arg.html#c.PyArg_VaParseTupleAndKeywords" title="PyArg_VaParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_VaParseTupleAndKeywords()</span></code></a>, is the same as
<a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> but takes a <code class="xref c c-type docutils literal notranslate"><span class="pre">va_list</span></code> instead of a
number of arguments. (Contributed by Greg Chapman.)</p></li>
<li><p>A new method flag, <code class="xref py py-const docutils literal notranslate"><span class="pre">METH_COEXISTS</span></code>, allows a function defined in slots
to co-exist with a <a class="reference internal" href="../c-api/structures.html#c.PyCFunction" title="PyCFunction"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCFunction</span></code></a> having the same name. This can halve
the access time for a method such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">set.__contains__()</span></code>. (Contributed by
Raymond Hettinger.)</p></li>
<li><p>Python can now be built with additional profiling for the interpreter itself,
intended as an aid to people developing the Python core. Providing
<code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-profiling</span></code> to the <strong class="program">configure</strong> script will let you
profile the interpreter with <strong class="program">gprof</strong>, and providing the
<code class="xref std std-option docutils literal notranslate"><span class="pre">--with-tsc</span></code> switch enables profiling using the Pentium's
Time-Stamp-Counter register. Note that the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-tsc</span></code> switch is slightly
misnamed, because the profiling feature also works on the PowerPC platform,
though that processor architecture doesn't call that register &quot;the TSC
register&quot;. (Contributed by Jeremy Hylton.)</p></li>
<li><p>The <code class="xref c c-type docutils literal notranslate"><span class="pre">tracebackobject</span></code> type has been renamed to
<code class="xref c c-type docutils literal notranslate"><span class="pre">PyTracebackObject</span></code>.</p></li>
</ul>
<section id="port-specific-changes">
<h3>移植专属的改变<a class="headerlink" href="#port-specific-changes" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>The Windows port now builds under MSVC++ 7.1 as well as version 6.
(Contributed by Martin von Löwis.)</p></li>
</ul>
</section>
</section>
<section id="porting-to-python-2-4">
<h2>移植到 Python 2.4<a class="headerlink" href="#porting-to-python-2-4" title="永久链接至标题"></a></h2>
<p>本节列出了先前描述的可能需要修改你的代码的改变:</p>
<ul class="simple">
<li><p>Left shifts and hexadecimal/octal constants that are too large no longer
trigger a <a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a> and return a value limited to 32 or 64 bits;
instead they return a long integer.</p></li>
<li><p>Integer operations will no longer trigger an <code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code>. The
<code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowWarning</span></code> warning will disappear in Python 2.5.</p></li>
<li><p>The <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> built-in function and <code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.izip()</span></code> now return an
empty list instead of raising a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception if called with no
arguments.</p></li>
<li><p>You can no longer compare the <code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code> and <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances
provided by the <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a> module. Two instances of different classes
will now always be unequal, and relative comparisons (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>) will raise
a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">dircache.listdir()</span></code> now passes exceptions to the caller instead of
returning empty lists.</p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">LexicalHandler.startDTD()</span></code> used to receive the public and system IDs in
the wrong order. This has been corrected; applications relying on the wrong
order need to be fixed.</p></li>
<li><p>现在 <a class="reference internal" href="../library/fcntl.html#fcntl.ioctl" title="fcntl.ioctl"><code class="xref py py-func docutils literal notranslate"><span class="pre">fcntl.ioctl()</span></code></a> 会在 <em>mutate</em> 参数被省略并且将造成影响时发出警告。</p></li>
<li><p>现在 <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> 模块默认将生成 GNU 格式的 tar 文件。</p></li>
<li><p>Encountering a failure while importing a module no longer leaves a
partially-initialized module object in <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>.</p></li>
<li><p>现在 <a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 是一个常量;将一个新值绑定到 <code class="docutils literal notranslate"><span class="pre">None</span></code> 将导致语法错误。</p></li>
<li><p>现在对于某些非法的值 <code class="xref py py-func docutils literal notranslate"><span class="pre">signals.signal()</span></code> 函数将引发 <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> 异常;在之前版本中这些错误会静默地放过。 例如,你将不能在 <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGKILL</span></code> 信号上设置处理器。</p></li>
</ul>
</section>
<section id="acknowledgements">
<span id="acks"></span><h2>致谢<a class="headerlink" href="#acknowledgements" title="永久链接至标题"></a></h2>
<p>作者感谢以下人员对本文各种草稿给予的建议更正和协助Koray Can, Hye-Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson, Fredrik Lundh, Sean Reifschneider, Sadruddin Rejeb.</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Python 2.4 有什么新变化</a><ul>
<li><a class="reference internal" href="#pep-218-built-in-set-objects">PEP 218: 内置集合对象</a></li>
<li><a class="reference internal" href="#pep-237-unifying-long-integers-and-integers">PEP 237: 统一长整数和整数</a></li>
<li><a class="reference internal" href="#pep-289-generator-expressions">PEP 289: 生成器表达式</a></li>
<li><a class="reference internal" href="#pep-292-simpler-string-substitutions">PEP 292: 更简单的字符串替换</a></li>
<li><a class="reference internal" href="#pep-318-decorators-for-functions-and-methods">PEP 318: 函数和方法的装饰器</a></li>
<li><a class="reference internal" href="#pep-322-reverse-iteration">PEP 322: 反向迭代</a></li>
<li><a class="reference internal" href="#pep-324-new-subprocess-module">PEP 324: 新的子进程模块</a></li>
<li><a class="reference internal" href="#pep-327-decimal-data-type">PEP 327: 十进制数据类型</a><ul>
<li><a class="reference internal" href="#why-is-decimal-needed">为什么需要十进制?</a></li>
<li><a class="reference internal" href="#the-decimal-type"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> 类型</a></li>
<li><a class="reference internal" href="#the-context-type"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code> 类型</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-328-multi-line-imports">PEP 328: 多行导入</a></li>
<li><a class="reference internal" href="#pep-331-locale-independent-float-string-conversions">PEP 331: Locale-Independent Float/String Conversions</a></li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a><ul>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a><ul>
<li><a class="reference internal" href="#cookielib">cookielib</a></li>
<li><a class="reference internal" href="#doctest">doctest</a></li>
</ul>
</li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a><ul>
<li><a class="reference internal" href="#port-specific-changes">移植专属的改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-2-4">移植到 Python 2.4</a></li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="2.5.html"
title="上一章">Python 2.5 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="2.3.html"
title="下一章">Python 2.3 有什么新变化</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.4.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="2.3.html" title="Python 2.3 有什么新变化"
>下一页</a> |</li>
<li class="right" >
<a href="2.5.html" title="Python 2.5 有什么新变化"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python的新变化</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>