957 lines
121 KiB
HTML
957 lines
121 KiB
HTML
|
||
<!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 3.0 有什么新变化 — 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.7 有什么新变化" href="2.7.html" />
|
||
<link rel="prev" title="Python 3.1 有什么新变化" href="3.1.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.0.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 3.0 有什么新变化</a><ul>
|
||
<li><a class="reference internal" href="#common-stumbling-blocks">常见的绊脚石</a><ul>
|
||
<li><a class="reference internal" href="#print-is-a-function">Print 是函数</a></li>
|
||
<li><a class="reference internal" href="#views-and-iterators-instead-of-lists">用视图和迭代器取代列表</a></li>
|
||
<li><a class="reference internal" href="#ordering-comparisons">排序比较</a></li>
|
||
<li><a class="reference internal" href="#integers">整数</a></li>
|
||
<li><a class="reference internal" href="#text-vs-data-instead-of-unicode-vs-8-bit">文本与数据而不是 Unicode 与 8 比特位</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#overview-of-syntax-changes">语法变化概述</a><ul>
|
||
<li><a class="reference internal" href="#new-syntax">新语法</a></li>
|
||
<li><a class="reference internal" href="#changed-syntax">语法变化</a></li>
|
||
<li><a class="reference internal" href="#removed-syntax">移除的语法</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#changes-already-present-in-python-2-6">Changes Already Present In Python 2.6</a></li>
|
||
<li><a class="reference internal" href="#library-changes">库的修改</a></li>
|
||
<li><a class="reference internal" href="#pep-3101-a-new-approach-to-string-formatting"><strong>PEP 3101</strong>: A New Approach To String Formatting</a></li>
|
||
<li><a class="reference internal" href="#changes-to-exceptions">Changes To Exceptions</a></li>
|
||
<li><a class="reference internal" href="#miscellaneous-other-changes">Miscellaneous Other Changes</a><ul>
|
||
<li><a class="reference internal" href="#operators-and-special-methods">Operators And Special Methods</a></li>
|
||
<li><a class="reference internal" href="#builtins">Builtins</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||
<li><a class="reference internal" href="#performance">性能</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-0">移植到 Python 3.0</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="3.1.html"
|
||
title="上一章">Python 3.1 有什么新变化</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="2.7.html"
|
||
title="下一章">Python 2.7 有什么新变化</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/3.0.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/3.0.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.7.html" title="Python 2.7 有什么新变化"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.1.html" title="Python 3.1 有什么新变化"
|
||
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> »</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> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python的新变化</a> »</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-3-0">
|
||
<h1>Python 3.0 有什么新变化<a class="headerlink" href="#what-s-new-in-python-3-0" title="永久链接至标题">¶</a></h1>
|
||
<dl class="field-list simple">
|
||
<dt class="field-odd">作者</dt>
|
||
<dd class="field-odd"><p>Guido van Rossum</p>
|
||
</dd>
|
||
</dl>
|
||
<p>This article explains the new features in Python 3.0, compared to 2.6.
|
||
Python 3.0, also known as "Python 3000" or "Py3K", is the first ever
|
||
<em>intentionally backwards incompatible</em> Python release. There are more
|
||
changes than in a typical release, and more that are important for all
|
||
Python users. Nevertheless, after digesting the changes, you'll find
|
||
that Python really hasn't changed all that much -- by and large, we're
|
||
mostly fixing well-known annoyances and warts, and removing a lot of
|
||
old cruft.</p>
|
||
<p>本文并不试图提供所有新特性的完整规范,而是试图提供一个方便的概述。 要了解完整的细节,您应该参考 Python 3.0 的文档和/或文中引用的许多 PEP。 如果您想了解某个特性的完整实现和设计原理,PEP 通常比常规文档有更多的细节;但要注意的是,一旦某个特性被完全实现,PEP 通常不会保持更新。</p>
|
||
<p>由于时间有限,本文档不够完整。 对于新发布的版本,源代码发行版中的 <code class="docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> 文件总是包含大量关于每一个细小改动的详细信息。</p>
|
||
<section id="common-stumbling-blocks">
|
||
<h2>常见的绊脚石<a class="headerlink" href="#common-stumbling-blocks" title="永久链接至标题">¶</a></h2>
|
||
<p>本节列出了在你已习惯了 Python 2.5 的情况下最有可能让您感到困惑的几处更改。</p>
|
||
<section id="print-is-a-function">
|
||
<h3>Print 是函数<a class="headerlink" href="#print-is-a-function" title="永久链接至标题">¶</a></h3>
|
||
<p><code class="docutils literal notranslate"><span class="pre">print</span></code> 语句已被 <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> 函数取代,其关键字参数取代了旧 <code class="docutils literal notranslate"><span class="pre">print</span></code> 语句 (<span class="target" id="index-48"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a>) 的大部分特殊语法。 示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="s2">"The answer is"</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"The answer is"</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="n">x</span><span class="p">,</span> <span class="c1"># Trailing comma suppresses newline</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" "</span><span class="p">)</span> <span class="c1"># Appends a space instead of a newline</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="c1"># Prints a newline</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">()</span> <span class="c1"># You must call the function!</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="o">>></span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s2">"fatal error"</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"fatal error"</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</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="c1"># prints repr((x, y))</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</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="c1"># Not the same as print(x, y)!</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>你还可以自定义条目间的分隔符,例如</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"There are <"</span><span class="p">,</span> <span class="mi">2</span><span class="o">**</span><span class="mi">32</span><span class="p">,</span> <span class="s2">"> possibilities!"</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这将产生如下结果:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>There are <4294967296> possibilities!
|
||
</pre></div>
|
||
</div>
|
||
<p>注意</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> 函数不支持旧 <code class="docutils literal notranslate"><span class="pre">print</span></code> 语句的 "softspace" 功能。例如,在 Python 2.x 中,<code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">"A\n",</span> <span class="pre">"B"</span></code> 会写入 <code class="docutils literal notranslate"><span class="pre">"A\nB\n"</span></code>;但在 Python 3.0 中,<code class="docutils literal notranslate"><span class="pre">print("A\n",</span> <span class="pre">"B")</span></code> 会写入 <code class="docutils literal notranslate"><span class="pre">"A\n</span> <span class="pre">B\n"</span></code>。</p></li>
|
||
<li><p>最初,您会发现自己在交互模式下经常输入旧的 <code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">x</span></code> 。是时候重新训练你的手指以输入 <code class="docutils literal notranslate"><span class="pre">print(x)</span></code> 了!</p></li>
|
||
<li><p>使用 <code class="docutils literal notranslate"><span class="pre">2to3</span></code> 源代码到源代码转换工具时,所有 <code class="docutils literal notranslate"><span class="pre">print</span></code> 语句都会自动转换为 <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> 函数调用,因此对于大型项目来说,这基本上不是问题。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="views-and-iterators-instead-of-lists">
|
||
<h3>用视图和迭代器取代列表<a class="headerlink" href="#views-and-iterators-instead-of-lists" title="永久链接至标题">¶</a></h3>
|
||
<p>某些知名的 API 将不再返回列表:</p>
|
||
<ul>
|
||
<li><p><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> 方法 <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a>、<a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a> 和 <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a> 返回 “视图” 而不是列表。 例如,这个写法不再有效: <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">=</span> <span class="pre">d.keys();</span> <span class="pre">k.sort()</span></code>。 请使用 <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">=</span> <span class="pre">sorted(d)</span></code> 代替(这在 Python 2.5 中也有效,而且同样高效)。</p></li>
|
||
<li><p>此外,不再支持 <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iterkeys()</span></code>、<code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iteritems()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.itervalues()</span></code> 方法。</p></li>
|
||
<li><p><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> 均返回迭代器。 如果你确实需要一个列表并且所有输入序列的长度相等,简单的解决办法是将 <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/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a> 中,例如 <code class="docutils literal notranslate"><span class="pre">list(map(...))</span></code>,但更好的办法通常是使用列表推导式(特别是当原始代码使用了 <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> 的时候),或是重写代码使得它完全不需要列表。 还有一种特殊技巧是将 <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="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 循环(因为创建列表会浪费资源)。</p>
|
||
<p>如果输入序列的长度不相等,<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> 将在最短序列的终点停止。 为了与 Python 2.x 中的 <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/itertools.html#itertools.zip_longest" title="itertools.zip_longest"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.zip_longest()</span></code></a> 中,例如将 <code class="docutils literal notranslate"><span class="pre">map(func,</span> <span class="pre">*sequences)</span></code> 变成 <code class="docutils literal notranslate"><span class="pre">list(map(func,</span> <span class="pre">itertools.zip_longest(*sequences)))</span></code>。</p>
|
||
</li>
|
||
<li><p>现在 <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> 的行为与过去 <code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code> 的行为类似,区别在于它可以处理任意大小的值。 后者已不复存在。</p></li>
|
||
<li><p><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> 现在将返回一个迭代器。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="ordering-comparisons">
|
||
<h3>排序比较<a class="headerlink" href="#ordering-comparisons" title="永久链接至标题">¶</a></h3>
|
||
<p>Python 3.0 简化了排序比较的规则:</p>
|
||
<ul class="simple">
|
||
<li><p>当操作数不存在有意义的自然排序时,排序比较操作符 (<code class="docutils literal notranslate"><span class="pre"><</span></code>, <code class="docutils literal notranslate"><span class="pre"><=</span></code>, <code class="docutils literal notranslate"><span class="pre">>=</span></code>, <code class="docutils literal notranslate"><span class="pre">></span></code>) 会引发 TypeError 异常。 因此,像 <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><</span> <span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">></span> <span class="pre">None</span></code> 或 <code class="docutils literal notranslate"><span class="pre">len</span> <span class="pre"><=</span> <span class="pre">len</span></code> 这样的表达式不再有效,例如 <code class="docutils literal notranslate"><span class="pre">None</span> <span class="pre"><</span> <span class="pre">None</span></code> 会引发 <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> 而不是返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。 由此推论,对异构列表进行排序不再有意义 —— 所有元素必须相互可比。 请注意,这不适用于 <code class="docutils literal notranslate"><span class="pre">==</span></code> 和 <code class="docutils literal notranslate"><span class="pre">!=</span></code> 操作符:不同的不可比类型的对象总是互不相等的。</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">builtin.sorted()</span></code> 和 <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> 不再接受提供比较函数的 <em>cmp</em> 参数。 请使用 <em>key</em> 参数。 注意 <em>key</em> 和 <em>reverse</em> 参数现在是“仅限关键字”参数。</p></li>
|
||
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> 函数应视为已不复存在,而 <code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> 特殊方法也不再支持。 请使用 <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></a> 进行排序,使用 <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> 与 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> 进行比较,并根据需要使用其他的丰富比较方法。 (如果确实需要 <code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> 功能,可以使用表达式 <code class="docutils literal notranslate"><span class="pre">(a</span> <span class="pre">></span> <span class="pre">b)</span> <span class="pre">-</span> <span class="pre">(a</span> <span class="pre"><</span> <span class="pre">b)</span></code> 以实现 <code class="docutils literal notranslate"><span class="pre">cmp(a,</span> <span class="pre">b)</span></code>。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="integers">
|
||
<h3>整数<a class="headerlink" href="#integers" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-49"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0237"><strong>PEP 237</strong></a>: 在实质上,<code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code> 已被重命名为 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>。 也就是说,现在只有一种内置整数类型,叫做 <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>;但其行为更像是旧的 <code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code> 类型。</p></li>
|
||
<li><p><span class="target" id="index-50"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a>: 像 <code class="docutils literal notranslate"><span class="pre">1/2</span></code> 这样的表达式将返回一个浮点数。 请使用 <code class="docutils literal notranslate"><span class="pre">1//2</span></code> 来得到取整的行为。 (后面这种语法已存在多年,至少从 Python 2.2 起就有了。)</p></li>
|
||
<li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxint</span></code> 常量已被删除,因为整数值不再有限制。 不过,<a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a> 仍可用作大于任何实际列表或字符串索引的整数。 它符合实现的“自然”整数大小,通常与同一平台上以前版本中的 <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxint</span></code> 相同(假设使用相同的构建选项)。</p></li>
|
||
<li><p>长整数的 <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 不再包括尾部的 <code class="docutils literal notranslate"><span class="pre">L</span></code>,因此无条件地删除该字符的代码会删除最后一位数字。 (请使用 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> 代替。)</p></li>
|
||
<li><p>八进制数字面值不再是 <code class="docutils literal notranslate"><span class="pre">0720</span></code> 的形式;而是改用 <code class="docutils literal notranslate"><span class="pre">0o720</span></code> 的形式。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="text-vs-data-instead-of-unicode-vs-8-bit">
|
||
<h3>文本与数据而不是 Unicode 与 8 比特位<a class="headerlink" href="#text-vs-data-instead-of-unicode-vs-8-bit" title="永久链接至标题">¶</a></h3>
|
||
<p>你对二进制数据和 Unicode 的所有认知都已改变。</p>
|
||
<ul class="simple">
|
||
<li><p>Python 3.0 uses the concepts of <em>text</em> and (binary) <em>data</em> instead
|
||
of Unicode strings and 8-bit strings. All text is Unicode; however
|
||
<em>encoded</em> Unicode is represented as binary data. The type used to
|
||
hold text is <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, the type used to hold data is
|
||
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>. The biggest difference with the 2.x situation is
|
||
that any attempt to mix text and data in Python 3.0 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>, whereas if you were to mix Unicode and 8-bit
|
||
strings in Python 2.x, it would work if the 8-bit string happened to
|
||
contain only 7-bit (ASCII) bytes, but you would get
|
||
<a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a> if it contained non-ASCII values. This
|
||
value-specific behavior has caused numerous sad faces over the
|
||
years.</p></li>
|
||
<li><p>As a consequence of this change in philosophy, pretty much all code
|
||
that uses Unicode, encodings or binary data most likely has to
|
||
change. The change is for the better, as in the 2.x world there
|
||
were numerous bugs having to do with mixing encoded and unencoded
|
||
text. To be prepared in Python 2.x, start using <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code>
|
||
for all unencoded text, and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> for binary or encoded data
|
||
only. Then the <code class="docutils literal notranslate"><span class="pre">2to3</span></code> tool will do most of the work for you.</p></li>
|
||
<li><p>你不能再使用 <code class="docutils literal notranslate"><span class="pre">u"..."</span></code> 字面值来表示 Unicode 文本。 不过,你必须使用 <code class="docutils literal notranslate"><span class="pre">b"..."</span></code> 字面值来表示二进制数据。</p></li>
|
||
<li><p>As the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> types cannot be mixed, you
|
||
must always explicitly convert between them. Use <a class="reference internal" href="../library/stdtypes.html#str.encode" title="str.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.encode()</span></code></a>
|
||
to go from <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> to <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#bytes.decode" title="bytes.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.decode()</span></code></a>
|
||
to go from <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> to <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. You can also use
|
||
<code class="docutils literal notranslate"><span class="pre">bytes(s,</span> <span class="pre">encoding=...)</span></code> and <code class="docutils literal notranslate"><span class="pre">str(b,</span> <span class="pre">encoding=...)</span></code>,
|
||
respectively.</p></li>
|
||
<li><p>与 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 一样,<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 类型是不可变的。 还有一个单独的 <em>mutable</em> 类型用于保存缓冲二进制数据,即 <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>。 几乎所有接受 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 的应用程序接口也接受 <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>。 可变 API 基于 <code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableSequence</span></code>。</p></li>
|
||
<li><p>原始字符串字面中的所有反斜线均按字面解释。 这意味着原始字符串中的 <code class="docutils literal notranslate"><span class="pre">'\U'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'\u'</span></code> 转义符不会被特殊处理。 例如,在 Python 3.0 中,<code class="docutils literal notranslate"><span class="pre">r'\u20ac'</span></code> 是一个包含 6 个字符的字符串,而在 2.6 中,<code class="docutils literal notranslate"><span class="pre">ur'\u20ac'</span></code> 是一个 “欧元” 字符。 (当然,这种变化只影响原始字符串的字面意义;在 Python 3.0 中,欧元字符是 <code class="docutils literal notranslate"><span class="pre">'\u20ac'</span></code>。)</p></li>
|
||
<li><p>内置的 <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> 抽象类型已被移除,请使用 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 代替。 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 和 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 类型在功能上没有足够的共通性,因此不需要共享基类。 <code class="docutils literal notranslate"><span class="pre">2to3</span></code> 工具(见下文)用 <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 替换了每一个 <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code>。</p></li>
|
||
<li><p>Files opened as text files (still the default mode for <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>)
|
||
always use an encoding to map between strings (in memory) and bytes
|
||
(on disk). Binary files (opened with a <code class="docutils literal notranslate"><span class="pre">b</span></code> in the mode argument)
|
||
always use bytes in memory. This means that if a file is opened
|
||
using an incorrect mode or encoding, I/O will likely fail loudly,
|
||
instead of silently producing incorrect data. It also means that
|
||
even Unix users will have to specify the correct mode (text or
|
||
binary) when opening a file. There is a platform-dependent default
|
||
encoding, which on Unixy platforms can be set with the <code class="docutils literal notranslate"><span class="pre">LANG</span></code>
|
||
environment variable (and sometimes also with some other
|
||
platform-specific locale-related environment variables). In many
|
||
cases, but not all, the system default is UTF-8; you should never
|
||
count on this default. Any application reading or writing more than
|
||
pure ASCII text should probably have a way to override the encoding.
|
||
There is no longer any need for using the encoding-aware streams
|
||
in the <a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> module.</p></li>
|
||
<li><p><a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>、<a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> 和 <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> 的初始值现在是仅 Unicode 的文本文件(即它们是 <a class="reference internal" href="../library/io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> 的实例)。 要使用这些数据流读写字节数据,需要使用它们的 <a class="reference internal" href="../library/io.html#io.TextIOBase.buffer" title="io.TextIOBase.buffer"><code class="xref py py-data docutils literal notranslate"><span class="pre">io.TextIOBase.buffer</span></code></a> 属性。</p></li>
|
||
<li><p>Filenames are passed to and returned from APIs as (Unicode) strings.
|
||
This can present platform-specific problems because on some
|
||
platforms filenames are arbitrary byte strings. (On the other hand,
|
||
on Windows filenames are natively stored as Unicode.) As a
|
||
work-around, most APIs (e.g. <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> and many functions in 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) that take filenames accept <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects
|
||
as well as strings, and a few APIs have a way to ask for a
|
||
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> return value. Thus, <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> returns a
|
||
list of <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> instances if the argument is a <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>
|
||
instance, and <a class="reference internal" href="../library/os.html#os.getcwdb" title="os.getcwdb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwdb()</span></code></a> returns the current working
|
||
directory as a <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> instance. Note that when
|
||
<a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> returns a list of strings, filenames that
|
||
cannot be decoded properly are omitted rather than raising
|
||
<a class="reference internal" href="../library/exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p></li>
|
||
<li><p>当系统提供的字节无法使用默认编码进行解释时,一些系统 API,如 <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> 和 <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a>,也会出现问题。 最好的办法可能是设置 <code class="docutils literal notranslate"><span class="pre">LANG</span></code> 变量并重新运行程序。</p></li>
|
||
<li><p><span class="target" id="index-51"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3138"><strong>PEP 3138</strong></a>: 字符串的 <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 将不再转义非 ASCII 字符。 不过,它仍然会转义控制字符和在 Unicode 标准中具有不可打印状态的码位。</p></li>
|
||
<li><p><span class="target" id="index-52"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3120"><strong>PEP 3120</strong></a>:现在默认的源码编码格式是UTF-8。</p></li>
|
||
<li><p><span class="target" id="index-53"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3131"><strong>PEP 3131</strong></a>: 现在允许在标识符中使用非 ASCII 字符(不过,标准库中的异常和注释中的贡献者名字仍然只使用 ASCII 字符)。</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">cStringIO</span></code> modules are gone. Instead,
|
||
import the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module and use <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.StringIO</span></code></a> or
|
||
<a class="reference internal" href="../library/io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> for text and data respectively.</p></li>
|
||
<li><p>另请参阅 <a class="reference internal" href="../howto/unicode.html#unicode-howto"><span class="std std-ref">Unicode 指南</span></a>,其内容已针对 Python 3.0 进行更新。</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="overview-of-syntax-changes">
|
||
<h2>语法变化概述<a class="headerlink" href="#overview-of-syntax-changes" title="永久链接至标题">¶</a></h2>
|
||
<p>本节提供了 Python 3.0 中每个 <em>语法</em> 变化的简要说明。</p>
|
||
<section id="new-syntax">
|
||
<h3>新语法<a class="headerlink" href="#new-syntax" title="永久链接至标题">¶</a></h3>
|
||
<ul>
|
||
<li><p><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3107"><strong>PEP 3107</strong></a>: Function argument and return value annotations. This
|
||
provides a standardized way of annotating a function's parameters
|
||
and return value. There are no semantics attached to such
|
||
annotations except that they can be introspected at runtime using
|
||
the <code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code> attribute. The intent is to encourage
|
||
experimentation through metaclasses, decorators or frameworks.</p></li>
|
||
<li><p><span class="target" id="index-54"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3102"><strong>PEP 3102</strong></a>:仅限关键字参数。在参数列表``*args`` 之后出现的命名参数 <em>必须</em> 在调用中使用关键字语法指定。也可以在参数列表中使用``*``来表示不接受长度可变的参数列表,但可以使用只包含关键字的参数。</p></li>
|
||
<li><p>类定义中的基类列表后允许使用关键字参数。 这是用于指定元类的新约定(见下一节),但也可用于其他目的,只要元类支持它。</p></li>
|
||
<li><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3104"><strong>PEP 3104</strong></a>: <a class="reference internal" href="../reference/simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> statement. Using <code class="docutils literal notranslate"><span class="pre">nonlocal</span> <span class="pre">x</span></code>
|
||
you can now assign directly to a variable in an outer (but
|
||
non-global) scope. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code> is a new reserved word.</p></li>
|
||
<li><p><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3132"><strong>PEP 3132</strong></a>: Extended Iterable Unpacking. You can now write things
|
||
like <code class="docutils literal notranslate"><span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">*rest</span> <span class="pre">=</span> <span class="pre">some_sequence</span></code>. And even <code class="docutils literal notranslate"><span class="pre">*rest,</span> <span class="pre">a</span> <span class="pre">=</span>
|
||
<span class="pre">stuff</span></code>. The <code class="docutils literal notranslate"><span class="pre">rest</span></code> object is always a (possibly empty) list; the
|
||
right-hand side may be any iterable. Example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This sets <em>a</em> to <code class="docutils literal notranslate"><span class="pre">0</span></code>, <em>b</em> to <code class="docutils literal notranslate"><span class="pre">4</span></code>, and <em>rest</em> to <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>.</p>
|
||
</li>
|
||
<li><p>Dictionary comprehensions: <code class="docutils literal notranslate"><span class="pre">{k:</span> <span class="pre">v</span> <span class="pre">for</span> <span class="pre">k,</span> <span class="pre">v</span> <span class="pre">in</span> <span class="pre">stuff}</span></code> means the
|
||
same thing as <code class="docutils literal notranslate"><span class="pre">dict(stuff)</span></code> but is more flexible. (This is
|
||
<span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0274"><strong>PEP 274</strong></a> vindicated. :-)</p></li>
|
||
<li><p>Set literals, e.g. <code class="docutils literal notranslate"><span class="pre">{1,</span> <span class="pre">2}</span></code>. Note that <code class="docutils literal notranslate"><span class="pre">{}</span></code> is an empty
|
||
dictionary; use <code class="docutils literal notranslate"><span class="pre">set()</span></code> for an empty set. Set comprehensions are
|
||
also supported; e.g., <code class="docutils literal notranslate"><span class="pre">{x</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">stuff}</span></code> means the same thing as
|
||
<code class="docutils literal notranslate"><span class="pre">set(stuff)</span></code> but is more flexible.</p></li>
|
||
<li><p>New octal literals, e.g. <code class="docutils literal notranslate"><span class="pre">0o720</span></code> (already in 2.6). The old octal
|
||
literals (<code class="docutils literal notranslate"><span class="pre">0720</span></code>) are gone.</p></li>
|
||
<li><p>New binary literals, e.g. <code class="docutils literal notranslate"><span class="pre">0b1010</span></code> (already in 2.6), and
|
||
there is a new corresponding built-in function, <a class="reference internal" href="../library/functions.html#bin" title="bin"><code class="xref py py-func docutils literal notranslate"><span class="pre">bin()</span></code></a>.</p></li>
|
||
<li><p>Bytes literals are introduced with a leading <code class="docutils literal notranslate"><span class="pre">b</span></code> or <code class="docutils literal notranslate"><span class="pre">B</span></code>, and
|
||
there is a new corresponding built-in function, <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytes()</span></code></a>.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changed-syntax">
|
||
<h3>语法变化<a class="headerlink" href="#changed-syntax" title="永久链接至标题">¶</a></h3>
|
||
<ul>
|
||
<li><p><span class="target" id="index-55"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a> 和 <span class="target" id="index-56"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: 新增 <a class="reference internal" href="../reference/simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> 语句的语法: <code class="samp docutils literal notranslate"><span class="pre">raise</span> <span class="pre">[</span><em><span class="pre">expr</span></em> <span class="pre">[from</span> <em><span class="pre">expr</span></em><span class="pre">]]</span></code>。 见下文。</p></li>
|
||
<li><p>现在 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> 和 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 是保留关键字。 (实际是从 2.6 开始。)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> 和 <code class="docutils literal notranslate"><span class="pre">None</span></code> 已成为保留关键字。 (2.6 已经对 <code class="docutils literal notranslate"><span class="pre">None</span></code> 部分强制应用限制。)</p></li>
|
||
<li><p>将 <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <em>exc</em>, <em>var</em> 改为 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code> <em>exc</em> <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em>。 参见 <span class="target" id="index-57"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>。</p></li>
|
||
<li><p><span class="target" id="index-58"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a>: 新的元类语法。 替换:</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="n">__metaclass__</span> <span class="o">=</span> <span class="n">M</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>你现在需要使用:</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="n">metaclass</span><span class="o">=</span><span class="n">M</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>不再支持全局模块变量 <code class="xref py py-data docutils literal notranslate"><span class="pre">__metaclass__</span></code>。 (它是一个“拐杖”,可以使默认使用新风格类变得更容易,而无需从 <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> 派生每个类。)</p>
|
||
</li>
|
||
<li><p>List comprehensions no longer support the syntactic form
|
||
<code class="samp docutils literal notranslate"><span class="pre">[...</span> <span class="pre">for</span> <em><span class="pre">var</span></em> <span class="pre">in</span> <em><span class="pre">item1</span></em><span class="pre">,</span> <em><span class="pre">item2</span></em><span class="pre">,</span> <span class="pre">...]</span></code>. Use
|
||
<code class="samp docutils literal notranslate"><span class="pre">[...</span> <span class="pre">for</span> <em><span class="pre">var</span></em> <span class="pre">in</span> <span class="pre">(</span><em><span class="pre">item1</span></em><span class="pre">,</span> <em><span class="pre">item2</span></em><span class="pre">,</span> <span class="pre">...)]</span></code> instead.
|
||
Also note that list comprehensions have different semantics: they
|
||
are closer to syntactic sugar for a generator expression inside a
|
||
<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> constructor, and in particular the loop control
|
||
variables are no longer leaked into the surrounding scope.</p></li>
|
||
<li><p><em>ellipsis</em> (<code class="docutils literal notranslate"><span class="pre">...</span></code>) 可以在任何地方作为原子表达式使用。(以前只允许在片段中使用。)另外,现在 <em>必须</em> 拼写为``...`` 。(以前也可以拼写为``. . .`` ,这只是一个偶然的语法。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="removed-syntax">
|
||
<h3>移除的语法<a class="headerlink" href="#removed-syntax" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-15"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3113"><strong>PEP 3113</strong></a>: Tuple parameter unpacking removed. You can no longer
|
||
write <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">foo(a,</span> <span class="pre">(b,</span> <span class="pre">c)):</span> <span class="pre">...</span></code>.
|
||
Use <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">foo(a,</span> <span class="pre">b_c):</span> <span class="pre">b,</span> <span class="pre">c</span> <span class="pre">=</span> <span class="pre">b_c</span></code> instead.</p></li>
|
||
<li><p>Removed backticks (use <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> instead).</p></li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre"><></span></code> (use <code class="docutils literal notranslate"><span class="pre">!=</span></code> instead).</p></li>
|
||
<li><p>Removed keyword: <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> is no longer a keyword; it remains as
|
||
a function. (Fortunately the function syntax was also accepted in
|
||
2.x.) Also note that <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> no longer takes a stream argument;
|
||
instead of <code class="docutils literal notranslate"><span class="pre">exec(f)</span></code> you can use <code class="docutils literal notranslate"><span class="pre">exec(f.read())</span></code>.</p></li>
|
||
<li><p>Integer literals no longer support a trailing <code class="docutils literal notranslate"><span class="pre">l</span></code> or <code class="docutils literal notranslate"><span class="pre">L</span></code>.</p></li>
|
||
<li><p>String literals no longer support a leading <code class="docutils literal notranslate"><span class="pre">u</span></code> or <code class="docutils literal notranslate"><span class="pre">U</span></code>.</p></li>
|
||
<li><p>The <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> <em>module</em> <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> <code class="docutils literal notranslate"><span class="pre">*</span></code> syntax is only
|
||
allowed at the module level, no longer inside functions.</p></li>
|
||
<li><p>The only acceptable syntax for relative imports is <code class="samp docutils literal notranslate"><span class="pre">from</span> <span class="pre">.[</span><em><span class="pre">module</span></em><span class="pre">]</span>
|
||
<span class="pre">import</span> <em><span class="pre">name</span></em></code>. 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> forms not starting with <code class="docutils literal notranslate"><span class="pre">.</span></code> are
|
||
interpreted as absolute imports. (<span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a>)</p></li>
|
||
<li><p>Classic classes are gone.</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="changes-already-present-in-python-2-6">
|
||
<h2>Changes Already Present In Python 2.6<a class="headerlink" href="#changes-already-present-in-python-2-6" title="永久链接至标题">¶</a></h2>
|
||
<p>Since many users presumably make the jump straight from Python 2.5 to
|
||
Python 3.0, this section reminds the reader of new features that were
|
||
originally designed for Python 3.0 but that were back-ported to Python
|
||
2.6. The corresponding sections in <a class="reference internal" href="2.6.html#whats-new-in-2-6"><span class="std std-ref">Python 2.6 有什么新变化</span></a> should be
|
||
consulted for longer descriptions.</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0343"><span class="std std-ref">PEP 343: "with" 语句</span></a>. The <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement is now a standard
|
||
feature and no longer needs to be imported from the <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>.
|
||
Also check out <a class="reference internal" href="2.6.html#new-26-context-managers"><span class="std std-ref">编写上下文管理器</span></a> and
|
||
<a class="reference internal" href="2.6.html#new-module-contextlib"><span class="std std-ref">contextlib 模块</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0366"><span class="std std-ref">PEP 366: 从主模块显式相对导入</span></a>. This enhances the usefulness of the <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>
|
||
option when the referenced module lives in a package.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0370"><span class="std std-ref">PEP 370: 分用户的 site-packages 目录</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0371"><span class="std std-ref">PEP 371: 多任务处理包</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3101"><span class="std std-ref">PEP 3101: 高级字符串格式</span></a>. Note: the 2.6 description mentions the
|
||
<a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method for both 8-bit and Unicode strings. In 3.0,
|
||
only the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type (text strings with Unicode support)
|
||
supports this method; the <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type does not. The plan is
|
||
to eventually make this the only API for string formatting, and to
|
||
start deprecating the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator in Python 3.1.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3105"><span class="std std-ref">PEP 3105: print 改为函数</span></a>. This is now a standard feature and no longer needs
|
||
to be imported from <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>. More details were given above.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3110"><span class="std std-ref">PEP 3110: 异常处理的变更</span></a>. The <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <em>exc</em> <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em>
|
||
syntax is now standard and <code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code> <em>exc</em>, <em>var</em> is no
|
||
longer supported. (Of course, the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em> part is still
|
||
optional.)</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3112"><span class="std std-ref">PEP 3112: 字节字面值</span></a>. The <code class="docutils literal notranslate"><span class="pre">b"..."</span></code> string literal notation (and its
|
||
variants like <code class="docutils literal notranslate"><span class="pre">b'...'</span></code>, <code class="docutils literal notranslate"><span class="pre">b"""..."""</span></code>, and <code class="docutils literal notranslate"><span class="pre">br"..."</span></code>) now
|
||
produces a literal of type <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3116"><span class="std std-ref">PEP 3116: 新 I/O 库</span></a>: <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> 模块现在是进行文件输入/输出的标准方法。 内置的 <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 函数现在是 <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a> 的别名,并增加了 <em>encoding</em>、<em>errors</em>、<em>newline</em> 和 <em>closefd</em> 等关键字参数。 还要注意的是,无效的 <em>mode</em> 参数现在会引发 <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>,而不是 <a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>。 文本文件对象底层的二进制文件对象可以像 <code class="xref py py-attr docutils literal notranslate"><span class="pre">f.buffer</span></code> 一样访问(但要注意,文本对象会为自己保留一个缓冲区,以加快编码和解码操作。)</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3118"><span class="std std-ref">PEP 3118: 修改缓冲区协议</span></a>. The old builtin <code class="xref py py-func docutils literal notranslate"><span class="pre">buffer()</span></code> is now really gone;
|
||
the new builtin <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a> provides (mostly) similar
|
||
functionality.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3119"><span class="std std-ref">PEP 3119: 抽象基类</span></a>. The <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to :pep:`3119`."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module and the ABCs defined in the
|
||
<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 plays a somewhat more prominent role in
|
||
the language now, and built-in collection types like <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>
|
||
and <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> conform to the <code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableMapping</span></code>
|
||
and <code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableSequence</span></code> ABCs, respectively.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3127"><span class="std std-ref">PEP 3127: 整型文字支持和语法</span></a>. As mentioned above, the new octal literal
|
||
notation is the only one supported, and binary literals have been
|
||
added.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3129"><span class="std std-ref">PEP 3129: 类装饰器</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3141"><span class="std std-ref">PEP 3141: 数字的类型层级结构</span></a>. The <a class="reference internal" href="../library/numbers.html#module-numbers" title="numbers: Numeric abstract base classes (Complex, Real, Integral, etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">numbers</span></code></a> module is another new use of
|
||
ABCs, defining Python's "numeric tower". Also note the new
|
||
<a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fractions</span></code></a> module which implements <a class="reference internal" href="../library/numbers.html#numbers.Rational" title="numbers.Rational"><code class="xref py py-class docutils literal notranslate"><span class="pre">numbers.Rational</span></code></a>.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="library-changes">
|
||
<h2>库的修改<a class="headerlink" href="#library-changes" title="永久链接至标题">¶</a></h2>
|
||
<p>Due to time constraints, this document does not exhaustively cover the
|
||
very extensive changes to the standard library. <span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> is the
|
||
reference for the major changes to the library. Here's a capsule
|
||
review:</p>
|
||
<ul>
|
||
<li><p>Many old modules were removed. Some, like <code class="xref py py-mod docutils literal notranslate"><span class="pre">gopherlib</span></code> (no
|
||
longer used) and <code class="xref py py-mod docutils literal notranslate"><span class="pre">md5</span></code> (replaced by <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a>), were
|
||
already deprecated by <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0004"><strong>PEP 4</strong></a>. Others were removed as a result
|
||
of the removal of support for various platforms such as Irix, BeOS
|
||
and Mac OS 9 (see <span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a>). Some modules were also selected for
|
||
removal in Python 3.0 due to lack of use or because a better
|
||
replacement exists. See <span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> for an exhaustive list.</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb3</span></code> package was removed because its presence in the
|
||
core standard library has proved over time to be a particular burden
|
||
for the core developers due to testing instability and Berkeley DB's
|
||
release schedule. However, the package is alive and well,
|
||
externally maintained at <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">https://www.jcea.es/programacion/pybsddb.htm</a>.</p></li>
|
||
<li><p>Some modules were renamed because their old name disobeyed
|
||
<span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a>, or for various other reasons. Here's the list:</p>
|
||
<table class="docutils align-default">
|
||
<colgroup>
|
||
<col style="width: 50%" />
|
||
<col style="width: 50%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>旧名称</p></th>
|
||
<th class="head"><p>新名称</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p>_winreg</p></td>
|
||
<td><p>winreg</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>ConfigParser</p></td>
|
||
<td><p>configparser</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>copy_reg</p></td>
|
||
<td><p>copyreg</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>Queue</p></td>
|
||
<td><p>queue</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>SocketServer</p></td>
|
||
<td><p>socketserver</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>markupbase</p></td>
|
||
<td><p>_markupbase</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>repr</p></td>
|
||
<td><p>reprlib</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>test.test_support</p></td>
|
||
<td><p>test.support</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</li>
|
||
<li><p>A common pattern in Python 2.x is to have one version of a module
|
||
implemented in pure Python, with an optional accelerated version
|
||
implemented as a C extension; for example, <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> and
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code>. This places the burden of importing the accelerated
|
||
version and falling back on the pure Python version on each user of
|
||
these modules. In Python 3.0, the accelerated versions are
|
||
considered implementation details of the pure Python versions.
|
||
Users should always import the standard version, which attempts to
|
||
import the accelerated version and falls back to the pure Python
|
||
version. The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> / <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> pair received this
|
||
treatment. The <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> module is on the list for 3.1. The
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code> module has been turned into a class in the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a>
|
||
module.</p></li>
|
||
<li><p>Some related modules have been grouped into packages, and usually
|
||
the submodule names have been simplified. The resulting new
|
||
packages are:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">anydbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dbhash</span></code>, <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">dumbdbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">gdbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">whichdb</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/html.html#module-html" title="html: Helpers for manipulating HTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">HTMLParser</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">htmlentitydefs</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">BaseHTTPServer</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">CGIHTTPServer</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> (all <code class="xref py py-mod docutils literal notranslate"><span class="pre">Tkinter</span></code>-related modules except
|
||
<a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a>). The target audience of <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> doesn't
|
||
really care about <a class="reference internal" href="../library/tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a>. Also note that as of Python
|
||
2.6, the functionality of <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> has been greatly enhanced.</p></li>
|
||
<li><p><a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> (<a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">robotparse</span></code>).</p></li>
|
||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc</span></code> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">DocXMLRPCServer</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleXMLRPCServer</span></code>).</p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<p>Some other changes to standard library modules, not covered by
|
||
<span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a>:</p>
|
||
<ul class="simple">
|
||
<li><p>Killed <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code>. Use the 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> class.</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module: removed <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_clear()</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_type</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_value</span></code>,
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_traceback</span></code>. (Note that <a class="reference internal" href="../library/sys.html#sys.last_type" title="sys.last_type"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.last_type</span></code></a>
|
||
etc. remain.)</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array.array</span></code></a> type: the <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> methods are gone; use <code class="xref py py-meth docutils literal notranslate"><span class="pre">fromfile()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">tofile()</span></code> instead. Also, the <code class="docutils literal notranslate"><span class="pre">'c'</span></code> typecode for array is
|
||
gone -- use either <code class="docutils literal notranslate"><span class="pre">'b'</span></code> for bytes or <code class="docutils literal notranslate"><span class="pre">'u'</span></code> for Unicode
|
||
characters.</p></li>
|
||
<li><p>Cleanup of 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: removed
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sequenceIncludes()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">isCallable()</span></code>.</p></li>
|
||
<li><p>Cleanup of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> module: <code class="xref py py-func docutils literal notranslate"><span class="pre">acquire_lock()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">release_lock()</span></code> are gone; use <code class="xref py py-func docutils literal notranslate"><span class="pre">acquire()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">release()</span></code> instead.</p></li>
|
||
<li><p>Cleanup of 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: removed the <code class="xref py py-func docutils literal notranslate"><span class="pre">jumpahead()</span></code> API.</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">new</span></code> module is gone.</p></li>
|
||
<li><p>The functions <code class="xref py py-func docutils literal notranslate"><span class="pre">os.tmpnam()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.tempnam()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">os.tmpfile()</span></code> have been removed in favor of the <a class="reference internal" href="../library/tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a>
|
||
module.</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/tokenize.html#module-tokenize" title="tokenize: Lexical scanner for Python source code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tokenize</span></code></a> module has been changed to work with bytes. The
|
||
main entry point is now <a class="reference internal" href="../library/tokenize.html#tokenize.tokenize" title="tokenize.tokenize"><code class="xref py py-func docutils literal notranslate"><span class="pre">tokenize.tokenize()</span></code></a>, instead of
|
||
generate_tokens.</p></li>
|
||
<li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">string.letters</span></code> and its friends (<code class="xref py py-data docutils literal notranslate"><span class="pre">string.lowercase</span></code> and
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">string.uppercase</span></code>) are gone. Use
|
||
<a class="reference internal" href="../library/string.html#string.ascii_letters" title="string.ascii_letters"><code class="xref py py-data docutils literal notranslate"><span class="pre">string.ascii_letters</span></code></a> etc. instead. (The reason for the
|
||
removal is that <code class="xref py py-data docutils literal notranslate"><span class="pre">string.letters</span></code> and friends had
|
||
locale-specific behavior, which is a bad idea for such
|
||
attractively-named global "constants".)</p></li>
|
||
<li><p>Renamed module <code class="xref py py-mod docutils literal notranslate"><span class="pre">__builtin__</span></code> to <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> (removing the
|
||
underscores, adding an 's'). The <code class="xref py py-data docutils literal notranslate"><span class="pre">__builtins__</span></code> variable
|
||
found in most global namespaces is unchanged. To modify a builtin,
|
||
you should use <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a>, not <code class="xref py py-data docutils literal notranslate"><span class="pre">__builtins__</span></code>!</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="pep-3101-a-new-approach-to-string-formatting">
|
||
<h2><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>: A New Approach To String Formatting<a class="headerlink" href="#pep-3101-a-new-approach-to-string-formatting" title="永久链接至标题">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>A new system for built-in string formatting operations replaces the
|
||
<code class="docutils literal notranslate"><span class="pre">%</span></code> string formatting operator. (However, the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator is
|
||
still supported; it will be deprecated in Python 3.1 and removed
|
||
from the language at some later time.) Read <span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a> for the full
|
||
scoop.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changes-to-exceptions">
|
||
<h2>Changes To Exceptions<a class="headerlink" href="#changes-to-exceptions" title="永久链接至标题">¶</a></h2>
|
||
<p>The APIs for raising and catching exception have been cleaned up and
|
||
new powerful features added:</p>
|
||
<ul>
|
||
<li><p><span class="target" id="index-25"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0352"><strong>PEP 352</strong></a>: All exceptions must be derived (directly or indirectly)
|
||
from <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a>. This is the root of the exception
|
||
hierarchy. This is not new as a recommendation, but the
|
||
<em>requirement</em> to inherit from <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> is new. (Python
|
||
2.6 still allowed classic classes to be raised, and placed no
|
||
restriction on what you can catch.) As a consequence, string
|
||
exceptions are finally truly and utterly dead.</p></li>
|
||
<li><p>Almost all exceptions should actually derive from <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>;
|
||
<a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> should only be used as a base class for
|
||
exceptions that should only be handled at the top level, such as
|
||
<a class="reference internal" href="../library/exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a> or <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>. The recommended
|
||
idiom for handling all exceptions except for this latter category is
|
||
to use <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p></li>
|
||
<li><p><code class="xref py py-exc docutils literal notranslate"><span class="pre">StandardError</span></code> was removed.</p></li>
|
||
<li><p>Exceptions no longer behave as sequences. Use the <code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code>
|
||
attribute instead.</p></li>
|
||
<li><p><span class="target" id="index-26"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a>: Raising exceptions. You must now use <code class="samp docutils literal notranslate"><span class="pre">raise</span>
|
||
<em><span class="pre">Exception</span></em><span class="pre">(</span><em><span class="pre">args</span></em><span class="pre">)</span></code> instead of <code class="samp docutils literal notranslate"><span class="pre">raise</span> <em><span class="pre">Exception</span></em><span class="pre">,</span> <em><span class="pre">args</span></em></code>.
|
||
Additionally, you can no longer explicitly specify a traceback;
|
||
instead, if you <em>have</em> to do this, you can assign directly to the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code> attribute (see below).</p></li>
|
||
<li><p><span class="target" id="index-27"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>: Catching exceptions. You must now use
|
||
<code class="samp docutils literal notranslate"><span class="pre">except</span> <em><span class="pre">SomeException</span></em> <span class="pre">as</span> <em><span class="pre">variable</span></em></code> instead
|
||
of <code class="samp docutils literal notranslate"><span class="pre">except</span> <em><span class="pre">SomeException</span></em><span class="pre">,</span> <em><span class="pre">variable</span></em></code>. Moreover, the
|
||
<em>variable</em> is explicitly deleted when the <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> block
|
||
is left.</p></li>
|
||
<li><p><span class="target" id="index-28"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: Exception chaining. There are two cases: implicit
|
||
chaining and explicit chaining. Implicit chaining happens when an
|
||
exception is raised in an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> or <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a>
|
||
handler block. This usually happens due to a bug in the handler
|
||
block; we call this a <em>secondary</em> exception. In this case, the
|
||
original exception (that was being handled) is saved as the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> attribute of the secondary exception.
|
||
Explicit chaining is invoked with this syntax:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="n">SecondaryException</span><span class="p">()</span> <span class="kn">from</span> <span class="nn">primary_exception</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(where <em>primary_exception</em> is any expression that produces an
|
||
exception object, probably an exception that was previously caught).
|
||
In this case, the primary exception is stored on the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> attribute of the secondary exception. The
|
||
traceback printed when an unhandled exception occurs walks the chain
|
||
of <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> attributes and prints a
|
||
separate traceback for each component of the chain, with the primary
|
||
exception at the top. (Java users may recognize this behavior.)</p>
|
||
</li>
|
||
<li><p><span class="target" id="index-29"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: Exception objects now store their traceback as the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code> attribute. This means that an exception
|
||
object now contains all the information pertaining to an exception,
|
||
and there are fewer reasons to use <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a> (though the
|
||
latter is not removed).</p></li>
|
||
<li><p>A few exception messages are improved when Windows fails to load an
|
||
extension module. For example, <code class="docutils literal notranslate"><span class="pre">error</span> <span class="pre">code</span> <span class="pre">193</span></code> is now <code class="docutils literal notranslate"><span class="pre">%1</span> <span class="pre">is</span>
|
||
<span class="pre">not</span> <span class="pre">a</span> <span class="pre">valid</span> <span class="pre">Win32</span> <span class="pre">application</span></code>. Strings now deal with non-English
|
||
locales.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="miscellaneous-other-changes">
|
||
<h2>Miscellaneous Other Changes<a class="headerlink" href="#miscellaneous-other-changes" title="永久链接至标题">¶</a></h2>
|
||
<section id="operators-and-special-methods">
|
||
<h3>Operators And Special Methods<a class="headerlink" href="#operators-and-special-methods" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">!=</span></code> now returns the opposite of <code class="docutils literal notranslate"><span class="pre">==</span></code>, unless <code class="docutils literal notranslate"><span class="pre">==</span></code> returns
|
||
<a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>.</p></li>
|
||
<li><p>The concept of "unbound methods" has been removed from the language.
|
||
When referencing a method as a class attribute, you now get a plain
|
||
function object.</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getslice__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__setslice__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__delslice__()</span></code>
|
||
were killed. The syntax <code class="docutils literal notranslate"><span class="pre">a[i:j]</span></code> now translates to
|
||
<code class="docutils literal notranslate"><span class="pre">a.__getitem__(slice(i,</span> <span class="pre">j))</span></code> (or <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a> or
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delitem__()</span></code></a>, when used as an assignment or deletion target,
|
||
respectively).</p></li>
|
||
<li><p><span class="target" id="index-30"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3114"><strong>PEP 3114</strong></a>: the standard <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> method has been renamed to
|
||
<a class="reference internal" href="../library/stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a>.</p></li>
|
||
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">__oct__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hex__()</span></code> special methods are removed
|
||
-- <a class="reference internal" href="../library/functions.html#oct" title="oct"><code class="xref py py-func docutils literal notranslate"><span class="pre">oct()</span></code></a> and <a class="reference internal" href="../library/functions.html#hex" title="hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">hex()</span></code></a> use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> now to convert
|
||
the argument to an integer.</p></li>
|
||
<li><p>Removed support for <code class="xref py py-attr docutils literal notranslate"><span class="pre">__members__</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__methods__</span></code>.</p></li>
|
||
<li><p>The function attributes named <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_X</span></code> have been renamed to
|
||
use the <code class="xref py py-data docutils literal notranslate"><span class="pre">__X__</span></code> form, freeing up these names in the function
|
||
attribute namespace for user-defined attributes. To wit,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_closure</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_code</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_defaults</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_dict</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_doc</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_globals</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_name</span></code> were renamed to <code class="xref py py-attr docutils literal notranslate"><span class="pre">__closure__</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__code__</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">__defaults__</span></code>, <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">__globals__</span></code>, <a class="reference internal" href="../library/stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>,
|
||
respectively.</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__nonzero__()</span></code> is now <a class="reference internal" href="../reference/datamodel.html#object.__bool__" title="object.__bool__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a>.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="builtins">
|
||
<h3>Builtins<a class="headerlink" href="#builtins" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-31"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3135"><strong>PEP 3135</strong></a>: New <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>. You can now invoke <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>
|
||
without arguments and (assuming this is in a regular instance method
|
||
defined inside a <a class="reference internal" href="../reference/compound_stmts.html#class"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">class</span></code></a> statement) the right class and
|
||
instance will automatically be chosen. With arguments, the behavior
|
||
of <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> is unchanged.</p></li>
|
||
<li><p><span class="target" id="index-32"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3111"><strong>PEP 3111</strong></a>: <code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code> was renamed to <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a>. That
|
||
is, the new <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> function reads a line from
|
||
<a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> and returns it with the trailing newline stripped.
|
||
It raises <a class="reference internal" href="../library/exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> if the input is terminated prematurely.
|
||
To get the old behavior of <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a>, use <code class="docutils literal notranslate"><span class="pre">eval(input())</span></code>.</p></li>
|
||
<li><p>A new built-in function <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> was added to call the
|
||
<a class="reference internal" href="../library/stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method on an object.</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/functions.html#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> function rounding strategy and return type have
|
||
changed. Exact halfway cases are now rounded to the nearest even
|
||
result instead of away from zero. (For example, <code class="docutils literal notranslate"><span class="pre">round(2.5)</span></code> now
|
||
returns <code class="docutils literal notranslate"><span class="pre">2</span></code> rather than <code class="docutils literal notranslate"><span class="pre">3</span></code>.) <code class="docutils literal notranslate"><span class="pre">round(x[,</span> <span class="pre">n])</span></code> now
|
||
delegates to <code class="docutils literal notranslate"><span class="pre">x.__round__([n])</span></code> instead of always returning a
|
||
float. It generally returns an integer when called with a single
|
||
argument and a value of the same type as <code class="docutils literal notranslate"><span class="pre">x</span></code> when called with two
|
||
arguments.</p></li>
|
||
<li><p>Moved <code class="xref py py-func docutils literal notranslate"><span class="pre">intern()</span></code> to <a class="reference internal" href="../library/sys.html#sys.intern" title="sys.intern"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.intern()</span></code></a>.</p></li>
|
||
<li><p>Removed: <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code>. Instead of <code class="docutils literal notranslate"><span class="pre">apply(f,</span> <span class="pre">args)</span></code> use
|
||
<code class="docutils literal notranslate"><span class="pre">f(*args)</span></code>.</p></li>
|
||
<li><p>Removed <a class="reference internal" href="../library/functions.html#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a>. Instead of <code class="docutils literal notranslate"><span class="pre">callable(f)</span></code> you can use
|
||
<code class="docutils literal notranslate"><span class="pre">isinstance(f,</span> <span class="pre">collections.Callable)</span></code>. The <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.isCallable()</span></code>
|
||
function is also gone.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">coerce()</span></code>. This function no longer serves a purpose
|
||
now that classic classes are gone.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code>. Instead of <code class="docutils literal notranslate"><span class="pre">execfile(fn)</span></code> use
|
||
<code class="docutils literal notranslate"><span class="pre">exec(open(fn).read())</span></code>.</p></li>
|
||
<li><p>Removed the <code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code> type. Use <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>. There are now several
|
||
different kinds of streams that open can return in the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code>. Use <a class="reference internal" href="../library/functools.html#functools.reduce" title="functools.reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.reduce()</span></code></a> if you really
|
||
need it; however, 99 percent of the time an explicit <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a>
|
||
loop is more readable.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code>. Use <a class="reference internal" href="../library/imp.html#imp.reload" title="imp.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">imp.reload()</span></code></a>.</p></li>
|
||
<li><p>Removed. <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.has_key()</span></code> -- use the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> operator
|
||
instead.</p></li>
|
||
</ul>
|
||
</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>Due to time constraints, here is a <em>very</em> incomplete list of changes
|
||
to the C API.</p>
|
||
<ul class="simple">
|
||
<li><p>Support for several platforms was dropped, including but not limited
|
||
to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.</p></li>
|
||
<li><p><span class="target" id="index-59"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a>: 新的缓冲区 API。</p></li>
|
||
<li><p><span class="target" id="index-34"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a>: Extension Module Initialization & Finalization.</p></li>
|
||
<li><p><span class="target" id="index-35"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3123"><strong>PEP 3123</strong></a>: Making <a class="reference internal" href="../c-api/structures.html#c.PyObject_HEAD" title="PyObject_HEAD"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_HEAD</span></code></a> conform to standard C.</p></li>
|
||
<li><p>No more C API support for restricted execution.</p></li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Coerce()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_CoerceEx()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyMember_Get()</span></code>, and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyMember_Set()</span></code> C API 已被移除。</p></li>
|
||
<li><p>New C API <a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModuleNoBlock" title="PyImport_ImportModuleNoBlock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleNoBlock()</span></code></a>, works like
|
||
<a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a> but won't block on the import lock
|
||
(returning an error instead).</p></li>
|
||
<li><p>Renamed the boolean conversion C-level slot and method:
|
||
<code class="docutils literal notranslate"><span class="pre">nb_nonzero</span></code> is now <code class="docutils literal notranslate"><span class="pre">nb_bool</span></code>.</p></li>
|
||
<li><p>从 C API 中移除 <code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_OLDARGS</span></code> 和 <code class="xref c c-macro docutils literal notranslate"><span class="pre">WITH_CYCLE_GC</span></code>。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="performance">
|
||
<h2>性能<a class="headerlink" href="#performance" title="永久链接至标题">¶</a></h2>
|
||
<p>The net result of the 3.0 generalizations is that Python 3.0 runs the
|
||
pystone benchmark around 10% slower than Python 2.5. Most likely the
|
||
biggest cause is the removal of special-casing for small integers.
|
||
There's room for improvement, but it will happen after 3.0 is
|
||
released!</p>
|
||
</section>
|
||
<section id="porting-to-python-3-0">
|
||
<h2>移植到 Python 3.0<a class="headerlink" href="#porting-to-python-3-0" title="永久链接至标题">¶</a></h2>
|
||
<p>For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
|
||
best strategy is the following:</p>
|
||
<ol class="arabic simple" start="0">
|
||
<li><p>(Prerequisite:) Start with excellent test coverage.</p></li>
|
||
<li><p>Port to Python 2.6. This should be no more work than the average
|
||
port from Python 2.x to Python 2.(x+1). Make sure all your tests
|
||
pass.</p></li>
|
||
<li><p>(Still using 2.6:) Turn on the <code class="xref std std-option docutils literal notranslate"><span class="pre">-3</span></code> command line switch.
|
||
This enables warnings about features that will be removed (or
|
||
change) in 3.0. Run your test suite again, and fix code that you
|
||
get warnings about until there are no warnings left, and all your
|
||
tests still pass.</p></li>
|
||
<li><p>Run the <code class="docutils literal notranslate"><span class="pre">2to3</span></code> source-to-source translator over your source code
|
||
tree. (See <a class="reference internal" href="../library/2to3.html#to3-reference"><span class="std std-ref">2to3 - 自动将 Python 2 代码转为 Python 3 代码</span></a> for more on this tool.) Run the
|
||
result of the translation under Python 3.0. Manually fix up any
|
||
remaining issues, fixing problems until all tests pass again.</p></li>
|
||
</ol>
|
||
<p>It is not recommended to try to write source code that runs unchanged
|
||
under both Python 2.6 and 3.0; you'd have to use a very contorted
|
||
coding style, e.g. avoiding <code class="docutils literal notranslate"><span class="pre">print</span></code> statements, metaclasses,
|
||
and much more. If you are maintaining a library that needs to support
|
||
both Python 2.6 and Python 3.0, the best approach is to modify step 3
|
||
above by editing the 2.6 version of the source code and running the
|
||
<code class="docutils literal notranslate"><span class="pre">2to3</span></code> translator again, rather than editing the 3.0 version of the
|
||
source code.</p>
|
||
<p>For porting C extensions to Python 3.0, please see <a class="reference internal" href="../howto/cporting.html#cporting-howto"><span class="std std-ref">将扩展模块移植到 Python 3</span></a>.</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 3.0 有什么新变化</a><ul>
|
||
<li><a class="reference internal" href="#common-stumbling-blocks">常见的绊脚石</a><ul>
|
||
<li><a class="reference internal" href="#print-is-a-function">Print 是函数</a></li>
|
||
<li><a class="reference internal" href="#views-and-iterators-instead-of-lists">用视图和迭代器取代列表</a></li>
|
||
<li><a class="reference internal" href="#ordering-comparisons">排序比较</a></li>
|
||
<li><a class="reference internal" href="#integers">整数</a></li>
|
||
<li><a class="reference internal" href="#text-vs-data-instead-of-unicode-vs-8-bit">文本与数据而不是 Unicode 与 8 比特位</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#overview-of-syntax-changes">语法变化概述</a><ul>
|
||
<li><a class="reference internal" href="#new-syntax">新语法</a></li>
|
||
<li><a class="reference internal" href="#changed-syntax">语法变化</a></li>
|
||
<li><a class="reference internal" href="#removed-syntax">移除的语法</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#changes-already-present-in-python-2-6">Changes Already Present In Python 2.6</a></li>
|
||
<li><a class="reference internal" href="#library-changes">库的修改</a></li>
|
||
<li><a class="reference internal" href="#pep-3101-a-new-approach-to-string-formatting"><strong>PEP 3101</strong>: A New Approach To String Formatting</a></li>
|
||
<li><a class="reference internal" href="#changes-to-exceptions">Changes To Exceptions</a></li>
|
||
<li><a class="reference internal" href="#miscellaneous-other-changes">Miscellaneous Other Changes</a><ul>
|
||
<li><a class="reference internal" href="#operators-and-special-methods">Operators And Special Methods</a></li>
|
||
<li><a class="reference internal" href="#builtins">Builtins</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||
<li><a class="reference internal" href="#performance">性能</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-0">移植到 Python 3.0</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="3.1.html"
|
||
title="上一章">Python 3.1 有什么新变化</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="2.7.html"
|
||
title="下一章">Python 2.7 有什么新变化</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/3.0.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.7.html" title="Python 2.7 有什么新变化"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.1.html" title="Python 3.1 有什么新变化"
|
||
>上一页</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> »</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> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python的新变化</a> »</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">
|
||
© <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> |