增加python文档
This commit is contained in:
979
web/python-docs/whatsnew/2.0.html
Normal file
979
web/python-docs/whatsnew/2.0.html
Normal file
@@ -0,0 +1,979 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||||
<head>
|
||||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||||
|
||||
<title>Python 2.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="更新日志" href="changelog.html" />
|
||||
<link rel="prev" title="Python 2.1 有什么新变化" href="2.1.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/2.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 2.0 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#introduction">概述</a></li>
|
||||
<li><a class="reference internal" href="#what-about-python-1-6">Python 1.6 将会怎样?</a></li>
|
||||
<li><a class="reference internal" href="#new-development-process">新开发流程</a></li>
|
||||
<li><a class="reference internal" href="#unicode">Unicode</a></li>
|
||||
<li><a class="reference internal" href="#list-comprehensions">列表推导式</a></li>
|
||||
<li><a class="reference internal" href="#augmented-assignment">增强赋值</a></li>
|
||||
<li><a class="reference internal" href="#string-methods">字符串的方法</a></li>
|
||||
<li><a class="reference internal" href="#garbage-collection-of-cycles">循环的垃圾回收</a></li>
|
||||
<li><a class="reference internal" href="#other-core-changes">其他核心变化</a><ul>
|
||||
<li><a class="reference internal" href="#minor-language-changes">细微的语言特性修改</a></li>
|
||||
<li><a class="reference internal" href="#changes-to-built-in-functions">对于内置函数的修改</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#porting-to-2-0">移植 Python 2.0</a></li>
|
||||
<li><a class="reference internal" href="#extending-embedding-changes">扩展/嵌入更改</a></li>
|
||||
<li><a class="reference internal" href="#distutils-making-modules-easy-to-install">Distutils:使模块易于安装</a></li>
|
||||
<li><a class="reference internal" href="#xml-modules">XML 模块</a><ul>
|
||||
<li><a class="reference internal" href="#sax2-support">SAX2 支持</a></li>
|
||||
<li><a class="reference internal" href="#dom-support">DOM 支持</a></li>
|
||||
<li><a class="reference internal" href="#relationship-to-pyxml">与 PyXML 的关系</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#module-changes">模块更改</a></li>
|
||||
<li><a class="reference internal" href="#new-modules">新增模块</a></li>
|
||||
<li><a class="reference internal" href="#idle-improvements">IDLE 改进</a></li>
|
||||
<li><a class="reference internal" href="#deleted-and-deprecated-modules">删除和弃用的模块</a></li>
|
||||
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="2.1.html"
|
||||
title="上一章">Python 2.1 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="changelog.html"
|
||||
title="下一章">更新日志</a></p>
|
||||
<div role="note" aria-label="source link">
|
||||
<h3>本页</h3>
|
||||
<ul class="this-page-menu">
|
||||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||||
<li>
|
||||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.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/2.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="changelog.html" title="更新日志"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="2.1.html" title="Python 2.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-2-0">
|
||||
<h1>Python 2.0 有什么新变化<a class="headerlink" href="#what-s-new-in-python-2-0" title="永久链接至标题">¶</a></h1>
|
||||
<dl class="field-list simple">
|
||||
<dt class="field-odd">作者</dt>
|
||||
<dd class="field-odd"><p>A.M. Kuchling 和 Moshe Zadka</p>
|
||||
</dd>
|
||||
</dl>
|
||||
<section id="introduction">
|
||||
<h2>概述<a class="headerlink" href="#introduction" title="永久链接至标题">¶</a></h2>
|
||||
<p>A new release of Python, version 2.0, was released on October 16, 2000. This
|
||||
article covers the exciting new features in 2.0, highlights some other useful
|
||||
changes, and points out a few incompatible changes that may require rewriting
|
||||
code.</p>
|
||||
<p>Python's development never completely stops between releases, and a steady flow
|
||||
of bug fixes and improvements are always being submitted. A host of minor fixes,
|
||||
a few optimizations, additional docstrings, and better error messages went into
|
||||
2.0; to list them all would be impossible, but they're certainly significant.
|
||||
Consult the publicly-available CVS logs if you want to see the full list. This
|
||||
progress is due to the five developers working for PythonLabs are now getting
|
||||
paid to spend their days fixing bugs, and also due to the improved communication
|
||||
resulting from moving to SourceForge.</p>
|
||||
</section>
|
||||
<section id="what-about-python-1-6">
|
||||
<h2>Python 1.6 将会怎样?<a class="headerlink" href="#what-about-python-1-6" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 1.6 可以被视为继续履行合同义务的 Python 发布版。 在核心开发团队于 2000 年 5 月离开 CNRI 之后,CNRI 要求创建一个 1.6 发布版,其中包含 CNRI 在 Python 项目上已完成的所有工作。 因此 Python 1.6 代表了截至 2000 年 5 月的 CVS 树的状态,其中最重要的新特性是 Unicode 支持。 当然,5 月之后开发工作仍在继续,因此 1.6 树接受了一些修正以确保它对 Python 2.0 向上兼容。 所以 1.6 仍是 Python 演化过程的组成部分,而不是一个旁支。</p>
|
||||
<p>那么,你应该对 Python 1.6 保持更多关注吗?也许不必。 1.6final 和 2.0beta1 是在同一天(2000 年 9 月 5 日)发布的,计划在一个月左右的时间内完成 Python 2.0 正式版。 如果你有应用程序需要维护,那么迁移到 1.6 破坏兼容性,修复它们,然后在一个月内又迁移到 2.0 再进行新一轮的兼容性修复的做法似乎没有什么意义;你最好直接迁移到 2.0。 本文档中介绍的大多数真正值得关注的特性都只出现在 2.0 中,因为很多工作都是在 5 月到 9 月之间完成的。</p>
|
||||
</section>
|
||||
<section id="new-development-process">
|
||||
<h2>新开发流程<a class="headerlink" href="#new-development-process" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 2.0 中最重要的变化可能根本不是代码,而是 Python 的开发方式:在 2000 年 5 月,Python 开发者开始使用 SourceForge 提供的工具来存储源代码、跟踪错误报告以及管理补丁提交队列。 要报告 Python 2.0 的错误或提交补丁,请使用位于 <a class="reference external" href="https://sourceforge.net/projects/python/">https://sourceforge.net/projects/python/</a> 的 Python项目页上的错误跟踪和补丁管理器工具。</p>
|
||||
<p>现在托管在SourceForge的最重要的服务是Python CVS树,这是一个包含Python源代码的版本控制库。以前,大约有7个左右的人可以写入CVS树,所有的补丁都必须由这个短名单上的一个人检查和签到。很明显,这并不是非常可扩展的。通过将CVS树转移到SourceForge,有可能向更多的人授予写访问权;截至2000年9月,有27人能够检查变化,增加了4倍。这使得大规模的改变成为可能,如果它们必须通过一小群核心开发者来过滤,就不会被尝试。例如,有一天Peter Schneider-Kamp想到了放弃K&R C的兼容性,将Python的C源转换为ANSI C。在获得python-dev邮件列表的批准后,他发起了一连串的签到,持续了大约一周,其他开发人员加入了进来帮忙,工作就完成了。如果只有5个人可以接触到写作,那么这项任务可能会被视为 "不错,但不值得花费时间和精力,而且它永远不会完成。</p>
|
||||
<p>转向使用 SourceForge 的服务显著提高了开发速度。 补丁现在由原提交者以外的人提交、评论、修改,并在不同人员之间来回传递,直到补丁被认为值得检入。 程序错误在一个中心位置被跟踪,并可以分配给特定人员进行修复,我们还可以统计未解决程序错误的数量来衡量进度。 这并不是没有代价的:开发人员现在需要处理更多的电子邮件,关注更多的邮件列表,并且需要为新环境编写专门的工具。 例如,SourceForge 发送的默认补丁和错误通知电子邮件完全无用,所以 Ka-Ping Yee 编写了一个 HTML 屏幕抓取器以便发送更有用的信息。</p>
|
||||
<p>添加代码的便利性引发了一些初期的成长痛苦,比如代码在准备好之前或未经开发者团队明确同意就被检入。 现在形成的审批流程有点类似于 Apache 团队所使用的流程。 开发者可以对补丁投票:+1、+0、-0 或 -1;+1 和 -1 表示接受或拒绝,而 +0 和 -0 则表示开发者对变更大多持无所谓的态度,但略有正面或负面的倾向。 与 Apache 模型最显著的变化是投票本质上是咨询性的,让拥有终身仁慈独裁者地位的 Guido van Rossum 了解总体意见。 他仍然可以忽略投票结果,并批准或拒绝变更,即使社区不同意他的决定。</p>
|
||||
<p>Producing an actual patch is the last step in adding a new feature, and is
|
||||
usually easy compared to the earlier task of coming up with a good design.
|
||||
Discussions of new features can often explode into lengthy mailing list threads,
|
||||
making the discussion hard to follow, and no one can read every posting to
|
||||
python-dev. Therefore, a relatively formal process has been set up to write
|
||||
Python Enhancement Proposals (PEPs), modelled on the Internet RFC process. PEPs
|
||||
are draft documents that describe a proposed new feature, and are continually
|
||||
revised until the community reaches a consensus, either accepting or rejecting
|
||||
the proposal. Quoting from the introduction to <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0001"><strong>PEP 1</strong></a>, "PEP Purpose and
|
||||
Guidelines":</p>
|
||||
<blockquote class="epigraph">
|
||||
<div><p>PEP 是 Python Enhancement Proposal 的缩写。 一个 PEP 就是一份设计文档,用来向 Python 社区提供信息,或描述一个 Python 新增特性。 PEP 应当提供对所提议特性的精确的技术规格和原理说明。</p>
|
||||
<p>我们打算将 PEP 作为提出新特性建议、收集社区对特定问题意见以及为必须加入 Python 的设计决策编写文档的首选机制。 PEP 的作者有责任在社区内部建立共识,并应将对立的观点也记入文档。</p>
|
||||
</div></blockquote>
|
||||
<p>Read the rest of <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0001"><strong>PEP 1</strong></a> for the details of the PEP editorial process, style, and
|
||||
format. PEPs are kept in the Python CVS tree on SourceForge, though they're not
|
||||
part of the Python 2.0 distribution, and are also available in HTML form from
|
||||
<a class="reference external" href="https://www.python.org/dev/peps/">https://www.python.org/dev/peps/</a>. As of September 2000, there are 25 PEPS, ranging
|
||||
from <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0201"><strong>PEP 201</strong></a>, "Lockstep Iteration", to PEP 225, "Elementwise/Objectwise
|
||||
Operators".</p>
|
||||
</section>
|
||||
<section id="unicode">
|
||||
<h2>Unicode<a class="headerlink" href="#unicode" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 2.0 中最大的新特性是引入了一种新的基本数据类型:Unicode 字符串。 Unicode 使用 16 位二进制数表示字符,而不是 ASCII 所使用的 8 位,这意味着可以支持 65,536 个不同的字符。</p>
|
||||
<p>Unicode 支持的最终接口是通过在 python-dev 邮件列表上无数次激烈的讨论达成的,主要由 Marc-André Lemburg 基于 Fredrik Lundh 的 Unicode 字符串类型实现来完成。 详细的接口说明被写成了 <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0100"><strong>PEP 100</strong></a> "Python Unicode Integration"。 这篇文章只简单地涵盖关于 Unicode 接口的最重要信息。</p>
|
||||
<p>In Python source code, Unicode strings are written as <code class="docutils literal notranslate"><span class="pre">u"string"</span></code>. Arbitrary
|
||||
Unicode characters can be written using a new escape sequence, <code class="docutils literal notranslate"><span class="pre">\uHHHH</span></code>, where
|
||||
<em>HHHH</em> is a 4-digit hexadecimal number from 0000 to FFFF. The existing
|
||||
<code class="docutils literal notranslate"><span class="pre">\xHHHH</span></code> escape sequence can also be used, and octal escapes can be used for
|
||||
characters up to U+01FF, which is represented by <code class="docutils literal notranslate"><span class="pre">\777</span></code>.</p>
|
||||
<p>Unicode 字符串和常规字符串一样,是一种不可变的序列类型。 它们可以被索引和切片,但不能原地修改。 Unicode 字符串有一个 <code class="docutils literal notranslate"><span class="pre">encode(</span> <span class="pre">[encoding]</span> <span class="pre">)</span></code> 方法,该方法返回一个以所需编码格式表示的 8 位字符串。 编码格式通过字符串命名,如 <code class="docutils literal notranslate"><span class="pre">'ascii'</span></code>、<code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code>、<code class="docutils literal notranslate"><span class="pre">'iso-8859-1'</span></code> 等等。 为实现和注册新的编码格式定义了一个编解码器 API,这些编码格式随后可在整个 Python 程序中使用。 如果未指定编码格式,默认编码格式通常是 7 位 ASCII,不过这可以通过在自定义版本的 <code class="file docutils literal notranslate"><span class="pre">site.py</span></code> 模块中调用 <code class="docutils literal notranslate"><span class="pre">sys.setdefaultencoding(encoding)</span></code> 函数来更改。</p>
|
||||
<p>将 8 比特位和 Unicode 的字符串结合使用时将总是会使用默认 ASCII 编码格式强制转换到 Unicode;<code class="docutils literal notranslate"><span class="pre">'a'</span> <span class="pre">+</span> <span class="pre">u'bc'</span></code> 的结果将为 <code class="docutils literal notranslate"><span class="pre">u'abc'</span></code>。</p>
|
||||
<p>新增了一批内置函数,现有的内置函数也被修改为支持 Unicode:</p>
|
||||
<ul class="simple">
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">unichr(ch)</span></code> 将返回长度为 1 个字符的 Unicode 字符串,其中包含字符 <em>ch</em>。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">ord(u)</span></code>,其中 <em>u</em> 是长度为 1 个字符的常规或 Unicode 字符串,将以整数形式返回该字符的码位值。</p></li>
|
||||
<li><p>函数 <code class="docutils literal notranslate"><span class="pre">unicode(string</span> <span class="pre">[,</span> <span class="pre">encoding]</span> <span class="pre">[,</span> <span class="pre">errors]</span> <span class="pre">)</span></code> 从 8 位字符串创建一个 Unicode 字符串。 <code class="docutils literal notranslate"><span class="pre">encoding</span></code> 是一个指定使用编码格式的字符串。<code class="docutils literal notranslate"><span class="pre">errors</span></code> 参数指定如何处理当前编码格式中无效的字符;传入 <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> 作为参数值会在有任何编码错误时引发异常,而 <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> 会静默忽略错误,<code class="docutils literal notranslate"><span class="pre">'replace'</span></code> 则在出现问题时使用 U+FFFD 即官方的替换字符。</p></li>
|
||||
<li><p><code class="docutils literal notranslate"><span class="pre">exec</span></code> 语句,以及各种内置函数如 <code class="docutils literal notranslate"><span class="pre">eval()</span></code>,<code class="docutils literal notranslate"><span class="pre">getattr()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">setattr()</span></code> 也会接受 Unicode 字符串和普通字符串。 (修复过程中可能会遗漏一些内置函数;如果你发现一个接受字符串但完全不接受 Unicode 字符串的内置函数,请报告此错误。)</p></li>
|
||||
</ul>
|
||||
<p>一个新的模块 <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> 提供了对 Unicode 字符属性的接口。 例如,<code class="docutils literal notranslate"><span class="pre">unicodedata.category(u'A')</span></code> 返回 2 个字符的字符串 'Lu',其中 'L' 表示这是一个字母,'u' 表示这是一个大写字母。 <code class="docutils literal notranslate"><span class="pre">unicodedata.bidirectional(u'\u0660')</span></code> 返回 'AN',表示 U+0660 是一个阿拉伯数字。</p>
|
||||
<p><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> 模块包含查找现有编码格式和注册新编码格式的函数。 除非你想实现一个新的编码格式,否则你最常使用的是 <code class="docutils literal notranslate"><span class="pre">codecs.lookup(encoding)</span></code> 函数,它返回一个 4 元素的元组: <code class="docutils literal notranslate"><span class="pre">(encode_func,</span> <span class="pre">decode_func,</span> <span class="pre">stream_reader,</span> <span class="pre">stream_writer)</span></code>。</p>
|
||||
<ul class="simple">
|
||||
<li><p><em>encode_func</em> 是一个接受 Unicode 字符串的函数,并返回一个 2 元组 <code class="docutils literal notranslate"><span class="pre">(string,</span> <span class="pre">length)</span></code>。 <em>string</em> 是一个包含部分(可能是全部) Unicode 字符串转换为指定编码的 8 位字符串,<em>length</em> 告诉你转换了多少 Unicode 字符串。</p></li>
|
||||
<li><p><em>decode_func</em> 与 <em>encode_func</em> 相反,它接受一个 8 位字符串并返回一个 2 元组 <code class="docutils literal notranslate"><span class="pre">(ustring,</span> <span class="pre">length)</span></code>,其中 <em>ustring</em> 是转换得到的 Unicode 字符串,<em>length</em> 是一个整数,表示消耗了多少 8 位字符串。</p></li>
|
||||
<li><p><em>stream_reader</em> is a class that supports decoding input from a stream.
|
||||
<em>stream_reader(file_obj)</em> returns an object that supports the <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code>,
|
||||
<a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">readlines()</span></code> methods. These methods will all
|
||||
translate from the given encoding and return Unicode strings.</p></li>
|
||||
<li><p><em>stream_writer</em>, similarly, is a class that supports encoding output to a
|
||||
stream. <em>stream_writer(file_obj)</em> returns an object that supports the
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">writelines()</span></code> methods. These methods expect Unicode
|
||||
strings, translating them to the given encoding on output.</p></li>
|
||||
</ul>
|
||||
<p>例如,以下的代码将 Unicode 字符串写入一个 UTF-8 编码的文件:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">codecs</span>
|
||||
|
||||
<span class="n">unistr</span> <span class="o">=</span> <span class="sa">u</span><span class="s1">'</span><span class="se">\u0660\u2000</span><span class="s1">ab ...'</span>
|
||||
|
||||
<span class="p">(</span><span class="n">UTF8_encode</span><span class="p">,</span> <span class="n">UTF8_decode</span><span class="p">,</span>
|
||||
<span class="n">UTF8_streamreader</span><span class="p">,</span> <span class="n">UTF8_streamwriter</span><span class="p">)</span> <span class="o">=</span> <span class="n">codecs</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s1">'UTF-8'</span><span class="p">)</span>
|
||||
|
||||
<span class="n">output</span> <span class="o">=</span> <span class="n">UTF8_streamwriter</span><span class="p">(</span> <span class="nb">open</span><span class="p">(</span> <span class="s1">'/tmp/output'</span><span class="p">,</span> <span class="s1">'wb'</span><span class="p">)</span> <span class="p">)</span>
|
||||
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span> <span class="n">unistr</span> <span class="p">)</span>
|
||||
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>以下的代码则可以从文件中读取 UTF-8 输入:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">input</span> <span class="o">=</span> <span class="n">UTF8_streamreader</span><span class="p">(</span> <span class="nb">open</span><span class="p">(</span> <span class="s1">'/tmp/output'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span> <span class="p">)</span>
|
||||
<span class="nb">print</span> <span class="nb">repr</span><span class="p">(</span><span class="nb">input</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
|
||||
<span class="nb">input</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>支持 Unicode 的正则表达式可以通过 <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> 模块使用,该模块有一个新的底层实现称为 SRE,由 Secret Labs AB 的 Fredrik Lundh 编写。</p>
|
||||
<p>添加了一个 <code class="docutils literal notranslate"><span class="pre">-U</span></code> 命令行选项,使 Python 编译器将所有字符串字面量解释为 Unicode 字符串字面量。 这用于测试和为你的 Python 代码提供未来保障,因为未来某个版本的 Python 可能会取消对 8 位字符串的支持,只提供 Unicode 字符串。</p>
|
||||
</section>
|
||||
<section id="list-comprehensions">
|
||||
<h2>列表推导式<a class="headerlink" href="#list-comprehensions" title="永久链接至标题">¶</a></h2>
|
||||
<p>列表是 Python 中的一种主力数据类型,许多程序在某个时候都会处理列表。 对列表的两种常见操作是遍历它们,并筛选出符合某个条件的元素,或对每个元素应用某个函数。 例如,给定一个字符串列表,你可能想要提取出所有包含特定子字符串的字符串,或去掉每行的尾随空白。</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> 和 <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> 函数可以用于此目的,但它们需要一个函数作为参数之一。 如果有一个现有的内置函数可以直接传递,这是很好的,但如果没有,你必须创建一个小函数来完成所需的工作。 而 Python 的作用域规则会使结果变得丑陋,特别是如果这个小函数需要额外的信息。 以上一段中的第一个例子为例,找到列表中所有包含给定子字符串的字符串。 你可以写如下代码来实现:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Given the list L, make a list of all strings</span>
|
||||
<span class="c1"># containing the substring S.</span>
|
||||
<span class="n">sublist</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span> <span class="k">lambda</span> <span class="n">s</span><span class="p">,</span> <span class="n">substring</span><span class="o">=</span><span class="n">S</span><span class="p">:</span>
|
||||
<span class="n">string</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">substring</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
|
||||
<span class="n">L</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>由于 Python 的作用域规则,将会使用默认参数以使由 <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> 表达式创建的匿名函数知道正在搜索哪个子字符串。 列表推导能使这个过程更简洁:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sublist</span> <span class="o">=</span> <span class="p">[</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">L</span> <span class="k">if</span> <span class="n">string</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">S</span><span class="p">)</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span> <span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>列表推导式的形式如下:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span> <span class="n">expression</span> <span class="k">for</span> <span class="n">expr</span> <span class="ow">in</span> <span class="n">sequence1</span>
|
||||
<span class="k">for</span> <span class="n">expr2</span> <span class="ow">in</span> <span class="n">sequence2</span> <span class="o">...</span>
|
||||
<span class="k">for</span> <span class="n">exprN</span> <span class="ow">in</span> <span class="n">sequenceN</span>
|
||||
<span class="k">if</span> <span class="n">condition</span> <span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code>...:keyword:<cite>!in</cite> 子句包含要迭代的序列。 这些序列不必具有相同的长度,因为它们 <em>不是</em> 并行迭代的,而是从左到右依次迭代;这一点将在以下段落中更清楚地解释。 生成列表的元素将是 <em>表达式</em> 的连续值。 最后的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code> 子句是可选的;如果存在,只有当 <em>condition</em> 为真时,<em>表达式</em> 才会被求值并添加到结果中。</p>
|
||||
<p>为了使语义更为清晰,列表推导相当于以下 Python 代码:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">expr1</span> <span class="ow">in</span> <span class="n">sequence1</span><span class="p">:</span>
|
||||
<span class="k">for</span> <span class="n">expr2</span> <span class="ow">in</span> <span class="n">sequence2</span><span class="p">:</span>
|
||||
<span class="o">...</span>
|
||||
<span class="k">for</span> <span class="n">exprN</span> <span class="ow">in</span> <span class="n">sequenceN</span><span class="p">:</span>
|
||||
<span class="k">if</span> <span class="p">(</span><span class="n">condition</span><span class="p">):</span>
|
||||
<span class="c1"># Append the value of</span>
|
||||
<span class="c1"># the expression to the</span>
|
||||
<span class="c1"># resulting list.</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>这意味着当有多个 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code>...:keyword:<cite>!in</cite> 子句时,生成的列表将等于所有序列长的的乘积。 如果你有两个长度为 3 的列表,输出列表将有 9 个元素:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">seq1</span> <span class="o">=</span> <span class="s1">'abc'</span>
|
||||
<span class="n">seq2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
|
||||
<span class="o">>>></span> <span class="p">[</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="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
|
||||
<span class="p">[(</span><span class="s1">'a'</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
|
||||
<span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>为了避免在 Python 的语法中引入歧义,如果 <em>表达式</em> 创建的是一个元组,它必须用括号括起来。 下面的第一个列表推导式有语法错误,而第二个则是正确的:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Syntax error</span>
|
||||
<span class="p">[</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
|
||||
<span class="c1"># Correct</span>
|
||||
<span class="p">[</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="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">seq1</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">seq2</span><span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>列表推导的概念最初来自函数式编程语言 Haskell (<a class="reference external" href="https://www.haskell.org">https://www.haskell.org</a>)。 Greg Ewing 最有力地提出了将其添加到 Python 中的建议,并编写了最初的列表推导式补丁,然后在 python-dev 邮件列表上进行了看似无休止的讨论,并由 Skip Montanaro 保持更新。</p>
|
||||
</section>
|
||||
<section id="augmented-assignment">
|
||||
<h2>增强赋值<a class="headerlink" href="#augmented-assignment" title="永久链接至标题">¶</a></h2>
|
||||
<p>增强赋值运算符,另一个长期以来要求添加的功能,已经被加入到 Python 2.0 中。 增强赋值运算符包括 <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">a</span> <span class="pre">+=</span> <span class="pre">2</span></code> 将变量 <code class="docutils literal notranslate"><span class="pre">a</span></code> 的值增加 2,等同于稍长一些的 <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">=</span> <span class="pre">a</span> <span class="pre">+</span> <span class="pre">2</span></code>。</p>
|
||||
<p>The full list of supported assignment operators is <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>, <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>, <code class="docutils literal notranslate"><span class="pre">^=</span></code>, <code class="docutils literal notranslate"><span class="pre">>>=</span></code>, and <code class="docutils literal notranslate"><span class="pre"><<=</span></code>. Python
|
||||
classes can override the augmented assignment operators by defining methods
|
||||
named <a class="reference internal" href="../reference/datamodel.html#object.__iadd__" title="object.__iadd__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iadd__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__isub__" title="object.__isub__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__isub__()</span></code></a>, etc. For example, the following
|
||||
<code class="xref py py-class docutils literal notranslate"><span class="pre">Number</span></code> class stores a number and supports using += to create a new
|
||||
instance with an incremented value.</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Number</span><span class="p">:</span>
|
||||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
|
||||
<span class="k">def</span> <span class="fm">__iadd__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">increment</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">Number</span><span class="p">(</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="n">increment</span><span class="p">)</span>
|
||||
|
||||
<span class="n">n</span> <span class="o">=</span> <span class="n">Number</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||||
<span class="n">n</span> <span class="o">+=</span> <span class="mi">3</span>
|
||||
<span class="nb">print</span> <span class="n">n</span><span class="o">.</span><span class="n">value</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The <a class="reference internal" href="../reference/datamodel.html#object.__iadd__" title="object.__iadd__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iadd__()</span></code></a> special method is called with the value of the increment,
|
||||
and should return a new instance with an appropriately modified value; this
|
||||
return value is bound as the new value of the variable on the left-hand side.</p>
|
||||
<p>增强赋值运算符最早在 C 编程语言中引入,大多数 C 派生语言,如 <strong class="program">awk</strong>,C++,Java 和 PHP 也支持它们。 增强赋值补丁由 Thomas Wouters 实现。</p>
|
||||
</section>
|
||||
<section id="string-methods">
|
||||
<h2>字符串的方法<a class="headerlink" href="#string-methods" title="永久链接至标题">¶</a></h2>
|
||||
<p>Until now string-manipulation functionality was in the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module,
|
||||
which was usually a front-end for the <code class="xref py py-mod docutils literal notranslate"><span class="pre">strop</span></code> module written in C. The
|
||||
addition of Unicode posed a difficulty for the <code class="xref py py-mod docutils literal notranslate"><span class="pre">strop</span></code> module, because the
|
||||
functions would all need to be rewritten in order to accept either 8-bit or
|
||||
Unicode strings. For functions such as <code class="xref py py-func docutils literal notranslate"><span class="pre">string.replace()</span></code>, which takes 3
|
||||
string arguments, that means eight possible permutations, and correspondingly
|
||||
complicated code.</p>
|
||||
<p>相反,Python 2.0 将这个问题推给了字符串类型,使字符串操作功能通过 8 位字符串和 Unicode 字符串上的方法来实现。</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'andrew'</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
|
||||
<span class="go">'Andrew'</span>
|
||||
<span class="gp">>>> </span><span class="s1">'hostname'</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'os'</span><span class="p">,</span> <span class="s1">'linux'</span><span class="p">)</span>
|
||||
<span class="go">'hlinuxtname'</span>
|
||||
<span class="gp">>>> </span><span class="s1">'moshe'</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">'sh'</span><span class="p">)</span>
|
||||
<span class="go">2</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>有一件事没有改变,即使是值得注意的愚人节玩笑,Python 字符串仍然是不可变的。 因此,字符串方法返回的是新的字符串,而不是修改他们操作的原字符串。</p>
|
||||
<p>旧的 <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> 模块仍然存在以保持向向兼容,但它主要作为新字符串方法的前端。</p>
|
||||
<p>Two methods which have no parallel in pre-2.0 versions, although they did exist
|
||||
in JPython for quite some time, are <code class="xref py py-meth docutils literal notranslate"><span class="pre">startswith()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">endswith()</span></code>.
|
||||
<code class="docutils literal notranslate"><span class="pre">s.startswith(t)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">s[:len(t)]</span> <span class="pre">==</span> <span class="pre">t</span></code>, while
|
||||
<code class="docutils literal notranslate"><span class="pre">s.endswith(t)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">s[-len(t):]</span> <span class="pre">==</span> <span class="pre">t</span></code>.</p>
|
||||
<p>One other method which deserves special mention is <code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code>. The
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code> method of a string receives one parameter, a sequence of strings,
|
||||
and is equivalent to the <code class="xref py py-func docutils literal notranslate"><span class="pre">string.join()</span></code> function from the old <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a>
|
||||
module, with the arguments reversed. In other words, <code class="docutils literal notranslate"><span class="pre">s.join(seq)</span></code> is
|
||||
equivalent to the old <code class="docutils literal notranslate"><span class="pre">string.join(seq,</span> <span class="pre">s)</span></code>.</p>
|
||||
</section>
|
||||
<section id="garbage-collection-of-cycles">
|
||||
<h2>循环的垃圾回收<a class="headerlink" href="#garbage-collection-of-cycles" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 的 C 实现使用引用技术来实现垃圾回收。 每个 Python 对象维护一个指向自身的引用数量,并在引用创建或销毁时调整该计数。 一旦引用计数达到零,对象就不再可访问,因为访问对象需要一个引用,然后如果计数为零,就不再存在任何引用。</p>
|
||||
<p>引用计数有一些令人愉快的特性:它易于理解和实现,结果实现是可移植的,相当快,并且与其他实现自己内存处理方案的库良好互动。引用计数的主要问题是有时它无法识别对象不再可访问,从而导致内存泄露,这发生在存在引用循环时。</p>
|
||||
<p>考虑最简单的循环,一个类实例引用自身:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">instance</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
|
||||
<span class="n">instance</span><span class="o">.</span><span class="n">myself</span> <span class="o">=</span> <span class="n">instance</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>在执行完上述两行代码后,<code class="docutils literal notranslate"><span class="pre">instance</span></code> 的引用计数是 2;一个引用来自名为 <code class="docutils literal notranslate"><span class="pre">'instance'</span></code> 的变量,另一个引用来自该实例的 <code class="docutils literal notranslate"><span class="pre">myself</span></code> 属性。</p>
|
||||
<p>如果下一行代码是 <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">instance</span></code>,会发生什么? <code class="docutils literal notranslate"><span class="pre">instance</span></code> 的引用计数会减少 1,所以它的引用计数变为 1;<code class="docutils literal notranslate"><span class="pre">myself</span></code> 属性中的引用仍然存在。 然而,该实例不能再通过 Python 代码访问,并且它可以被删除。 如果多个对象相互引用,它们可以参与一个循环,导致所有对象都无法被垃圾回收,从而导致内存泄漏。</p>
|
||||
<p>Python 2.0 通过周期性的执行一个循环检测算法来解决这个问题,该算法查找不可访问的循环并删除涉及的对象。 一个新的 <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> 模块提供了执行垃圾回收、获取调试统计信息和调整回收器参数的功能。</p>
|
||||
<p>运行循环检测算法需要一些时间,因此会带来一些额外的开销,希望在使用 2.0 版本的循环收集经验之后,Python 2.1 可以通过精细调整来尽量减少开销。 目前还不清楚性能损失有多大,因为对此进行精准测试很棘手,而且关键在于程序创建和销毁对象的频率。 如果你不能接受哪怕是微小的速度损失,或者怀疑循环收集存在问题,可以在编译 Python 时禁用循环检测,通过在运行 <strong class="program">configure</strong> 脚本时指定 <code class="xref std std-option docutils literal notranslate"><span class="pre">--without-cycle-gc</span></code> 开关来实现。</p>
|
||||
<p>有几个人解决了这个问题并为解决方案做出了贡献。循环检测方法的早期实现由 Toby Kelsey 编写。 当前的算法是在 Eric Tiedemann 访问 CNRI 期间提出的,Guido van Rossum 和 Neil Schemenauer 分别编写了两个不同的实现,后来由 Neil 将它们整合。 许多其他人也在过程中提出了建议;python-dev 邮件列表 2000 年 3 月的存档包含了大部分相关讨论,尤其是在标题为 “Reference cycle collection for Python” 和 “Finalization again” 的帖子中。</p>
|
||||
</section>
|
||||
<section id="other-core-changes">
|
||||
<h2>其他核心变化<a class="headerlink" href="#other-core-changes" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 的语法和内置函数进行了各种小改动。 虽然这些改动都不是非常深远,但它们都是很方便的改进。</p>
|
||||
<section id="minor-language-changes">
|
||||
<h3>细微的语言特性修改<a class="headerlink" href="#minor-language-changes" title="永久链接至标题">¶</a></h3>
|
||||
<p>A new syntax makes it more convenient to call a given function with a tuple of
|
||||
arguments and/or a dictionary of keyword arguments. In Python 1.5 and earlier,
|
||||
you'd use the <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code> built-in function: <code class="docutils literal notranslate"><span class="pre">apply(f,</span> <span class="pre">args,</span> <span class="pre">kw)</span></code> calls the
|
||||
function <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code> with the argument tuple <em>args</em> and the keyword arguments in
|
||||
the dictionary <em>kw</em>. <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code> is the same in 2.0, but thanks to a patch
|
||||
from Greg Ewing, <code class="docutils literal notranslate"><span class="pre">f(*args,</span> <span class="pre">**kw)</span></code> is a shorter and clearer way to achieve the
|
||||
same effect. This syntax is symmetrical with the syntax for defining
|
||||
functions:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
|
||||
<span class="c1"># args is a tuple of positional args,</span>
|
||||
<span class="c1"># kw is a dictionary of keyword args</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The <code class="docutils literal notranslate"><span class="pre">print</span></code> statement can now have its output directed to a file-like
|
||||
object by following the <code class="docutils literal notranslate"><span class="pre">print</span></code> with <code class="docutils literal notranslate"><span class="pre">>></span> <span class="pre">file</span></code>, similar to the
|
||||
redirection operator in Unix shells. Previously you'd either have to use the
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method of the file-like object, which lacks the convenience and
|
||||
simplicity of <code class="docutils literal notranslate"><span class="pre">print</span></code>, or you could assign a new value to
|
||||
<code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and then restore the old value. For sending output to standard
|
||||
error, it's much easier to write this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></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">"Warning: action field not supplied"</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>模块现在可以在导入时重命名,使用语法 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">module</span> <span class="pre">as</span> <span class="pre">name</span></code> 或 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">name</span> <span class="pre">as</span> <span class="pre">othername</span></code>。这个补丁是由 Thomas Wouters 提交的。</p>
|
||||
<p>当使用 <code class="docutils literal notranslate"><span class="pre">%</span></code> 操作符时有一种新的格式样式可用;'%r' 将插入其参数的 <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> 表示。 这也是为了对称性,这次是为了与现有的 %s 格式样式对称,后者插入其参数的 <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> 表示。例如,<code class="docutils literal notranslate"><span class="pre">'%r</span> <span class="pre">%s'</span> <span class="pre">%</span> <span class="pre">('abc',</span> <span class="pre">'abc')</span></code> 返回一个包含 <code class="docutils literal notranslate"><span class="pre">'abc'</span> <span class="pre">abc</span></code> 的字符串。</p>
|
||||
<p>Previously there was no way to implement a class that overrode Python's built-in
|
||||
<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 and implemented a custom version. <code class="docutils literal notranslate"><span class="pre">obj</span> <span class="pre">in</span> <span class="pre">seq</span></code> returns
|
||||
true if <em>obj</em> is present in the sequence <em>seq</em>; Python computes this by simply
|
||||
trying every index of the sequence until either <em>obj</em> is found or an
|
||||
<a class="reference internal" href="../library/exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> is encountered. Moshe Zadka contributed a patch which adds a
|
||||
<a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a> magic method for providing a custom implementation for
|
||||
<code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code>. Additionally, new built-in objects written in C can define what
|
||||
<code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code> means for them via a new slot in the sequence protocol.</p>
|
||||
<p>早期版本的 Python 使用递归算法来删除对象。深度嵌套的数据结构可能导致解释器填满 C 栈并崩溃。 Christian Tismer 重写了删除逻辑来解决这个问题。 相关地,比较递归对象时会导致无线递归并崩溃。Jeremy Hylton 重写了代码,使其不再崩溃,而是产生有用的结果。 例如,在以下代码之后:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="n">b</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
|
||||
<span class="n">b</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The comparison <code class="docutils literal notranslate"><span class="pre">a==b</span></code> returns true, because the two recursive data structures
|
||||
are isomorphic. See the thread "trashcan and PR#7" in the April 2000 archives of
|
||||
the python-dev mailing list for the discussion leading up to this
|
||||
implementation, and some useful relevant links. Note that comparisons can now
|
||||
also raise exceptions. In earlier versions of Python, a comparison operation
|
||||
such as <code class="docutils literal notranslate"><span class="pre">cmp(a,b)</span></code> would always produce an answer, even if a user-defined
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method encountered an error, since the resulting exception would
|
||||
simply be silently swallowed.</p>
|
||||
<p>Work has been done on porting Python to 64-bit Windows on the Itanium processor,
|
||||
mostly by Trent Mick of ActiveState. (Confusingly, <code class="docutils literal notranslate"><span class="pre">sys.platform</span></code> is still
|
||||
<code class="docutils literal notranslate"><span class="pre">'win32'</span></code> on Win64 because it seems that for ease of porting, MS Visual C++
|
||||
treats code as 32 bit on Itanium.) PythonWin also supports Windows CE; see the
|
||||
Python CE page at <a class="reference external" href="http://pythonce.sourceforge.net/">http://pythonce.sourceforge.net/</a> for more information.</p>
|
||||
<p>另一个新的平台是 Darwin/MacOS X;Python 2.0 中提供了初步支持。如果你指定 "configure --with-dyld --with-suffix=.x",动态加载是可行的。 有关更多说明,请参阅 Python 源代码分发中的 README 文件。</p>
|
||||
<p>已经尝试解决 Python 的一个问题,即当代码在局部变量赋值之前引用该变量时,会引发经常令人困惑的 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 异常。例如,以下代码在 1.5.2 和 2.0 中都会在 <code class="docutils literal notranslate"><span class="pre">print</span></code> 语句上引发异常;在 1.5.2 中,会引发 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 异常,而在 2.0 中,会引发一个新的 <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> 异常。 <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> 是 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 的子类,因此任何期望引发 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 的现有代码应该仍然可以正常工作。</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
|
||||
<span class="nb">print</span> <span class="s2">"i="</span><span class="p">,</span><span class="n">i</span>
|
||||
<span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
<span class="n">f</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>新引入了两个异常 <a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> 和 <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a>。 它们均为 <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> 的子类,并会在发现 Python 代码缩进不正确时被引发。</p>
|
||||
</section>
|
||||
<section id="changes-to-built-in-functions">
|
||||
<h3>对于内置函数的修改<a class="headerlink" href="#changes-to-built-in-functions" title="永久链接至标题">¶</a></h3>
|
||||
<p>添加了一个新的内置函数 <code class="docutils literal notranslate"><span class="pre">zip(seq1,</span> <span class="pre">seq2,</span> <span class="pre">...)</span></code>。<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> 返回一个包含元组的列表,每个元组包含每个参数序列的第i个元素。<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> 和 <code class="docutils literal notranslate"><span class="pre">map(None,</span> <span class="pre">seq1,</span> <span class="pre">seq2)</span></code> 的区别在于,如果序列长度不一致,<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> 会用 <code class="docutils literal notranslate"><span class="pre">None</span></code> 填充序列,而 <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>
|
||||
<p>The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> and <code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code> functions now accept an optional "base"
|
||||
parameter when the first argument is a string. <code class="docutils literal notranslate"><span class="pre">int('123',</span> <span class="pre">10)</span></code> returns 123,
|
||||
while <code class="docutils literal notranslate"><span class="pre">int('123',</span> <span class="pre">16)</span></code> returns 291. <code class="docutils literal notranslate"><span class="pre">int(123,</span> <span class="pre">16)</span></code> raises a
|
||||
<a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception with the message "can't convert non-string with
|
||||
explicit base".</p>
|
||||
<p>在 <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> 模块中添加了一个新变量,用于保存更详细的版本信息。 <code class="docutils literal notranslate"><span class="pre">sys.version_info</span></code> 是一个包含五个元素的元组 <code class="docutils literal notranslate"><span class="pre">(major,</span> <span class="pre">minor,</span> <span class="pre">micro,</span> <span class="pre">level,</span> <span class="pre">serial)</span></code>。 例如,在假设的 2.0.1beta1 版本中,<code class="docutils literal notranslate"><span class="pre">sys.version_info</span></code> 将是 <code class="docutils literal notranslate"><span class="pre">(2,</span> <span class="pre">0,</span> <span class="pre">1,</span> <span class="pre">'beta',</span> <span class="pre">1)</span></code>。 <em>level</em> 是一个字符串,如 <code class="docutils literal notranslate"><span class="pre">"alpha"</span></code>、<code class="docutils literal notranslate"><span class="pre">"beta"</span></code> 或代表最终发布版本的 <code class="docutils literal notranslate"><span class="pre">"final"</span></code>。</p>
|
||||
<p>Dictionaries have an odd new method, <code class="docutils literal notranslate"><span class="pre">setdefault(key,</span> <span class="pre">default)</span></code>, which
|
||||
behaves similarly to the existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> method. However, if the key is
|
||||
missing, <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> both returns the value of <em>default</em> as <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code>
|
||||
would do, and also inserts it into the dictionary as the value for <em>key</em>. Thus,
|
||||
the following lines of code:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="nb">dict</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span> <span class="n">key</span> <span class="p">):</span> <span class="k">return</span> <span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">return</span> <span class="nb">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>可以简化为单个``return dict.setdefault(key, [])``语句。</p>
|
||||
<p>解释器设置了一个最大递归深度,以便在填满 C 栈并导致核心储存或 GPF 之前捕获失控递归。以前这个限制是在编译 Python 时固定的,但在 2.0 中最大递归深度可以使用 <a class="reference internal" href="../library/sys.html#sys.getrecursionlimit" title="sys.getrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getrecursionlimit()</span></code></a> 和 <a class="reference internal" href="../library/sys.html#sys.setrecursionlimit" title="sys.setrecursionlimit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setrecursionlimit()</span></code></a> 读取和修改。 默认值是 1000,可以通过运行一个新脚本 <code class="file docutils literal notranslate"><span class="pre">Misc/find_recursionlimit.py</span></code> 来找到给定平台的大致的最大值。</p>
|
||||
</section>
|
||||
</section>
|
||||
<section id="porting-to-2-0">
|
||||
<h2>移植 Python 2.0<a class="headerlink" href="#porting-to-2-0" title="永久链接至标题">¶</a></h2>
|
||||
<p>新的 Python 版本尽力与之前的版本兼容,而且兼容性记录相当不错。 然而,有些变化被认为足够有用,通常是因为他们修正了最终设计中的错误决定,因此有时无法避免打破向后兼容性。 本节列出了 Python 2.0 中可能导致旧 Python 代码中断的更改。</p>
|
||||
<p>The change which will probably break the most code is tightening up the
|
||||
arguments accepted by some methods. Some methods would take multiple arguments
|
||||
and treat them as a tuple, particularly various list methods such as
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">insert()</span></code>. In earlier versions of Python, if <code class="docutils literal notranslate"><span class="pre">L</span></code> is
|
||||
a list, <code class="docutils literal notranslate"><span class="pre">L.append(</span> <span class="pre">1,2</span> <span class="pre">)</span></code> appends the tuple <code class="docutils literal notranslate"><span class="pre">(1,2)</span></code> to the list. In Python
|
||||
2.0 this causes a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception to be raised, with the message:
|
||||
'append requires exactly 1 argument; 2 given'. The fix is to simply add an
|
||||
extra set of parentheses to pass both values as a tuple: <code class="docutils literal notranslate"><span class="pre">L.append(</span> <span class="pre">(1,2)</span> <span class="pre">)</span></code>.</p>
|
||||
<p>The earlier versions of these methods were more forgiving because they used an
|
||||
old function in Python's C interface to parse their arguments; 2.0 modernizes
|
||||
them to use <code class="xref py py-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code>, the current argument parsing function,
|
||||
which provides more helpful error messages and treats multi-argument calls as
|
||||
errors. If you absolutely must use 2.0 but can't fix your code, you can edit
|
||||
<code class="file docutils literal notranslate"><span class="pre">Objects/listobject.c</span></code> and define the preprocessor symbol
|
||||
<code class="docutils literal notranslate"><span class="pre">NO_STRICT_LIST_APPEND</span></code> to preserve the old behaviour; this isn't recommended.</p>
|
||||
<p>Some of the functions in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module are still forgiving in this
|
||||
way. For example, <code class="xref py py-func docutils literal notranslate"><span class="pre">socket.connect(</span> <span class="pre">('hostname',</span> <span class="pre">25)</span> <span class="pre">)()</span></code> is the correct
|
||||
form, passing a tuple representing an IP address, but <code class="xref py py-func docutils literal notranslate"><span class="pre">socket.connect(</span>
|
||||
<span class="pre">'hostname',</span> <span class="pre">25</span> <span class="pre">)()</span></code> also works. <code class="xref py py-func docutils literal notranslate"><span class="pre">socket.connect_ex()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">socket.bind()</span></code>
|
||||
are similarly easy-going. 2.0alpha1 tightened these functions up, but because
|
||||
the documentation actually used the erroneous multiple argument form, many
|
||||
people wrote code which would break with the stricter checking. GvR backed out
|
||||
the changes in the face of public reaction, so for the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module, the
|
||||
documentation was fixed and the multiple argument form is simply marked as
|
||||
deprecated; it <em>will</em> be tightened up again in a future Python version.</p>
|
||||
<p>字符串字面量中的 <code class="docutils literal notranslate"><span class="pre">\x</span></code> 转义现在必须精确地使用2个十六进制数字。之前它会消耗 x 后面的所有十六进制数字,并取结果的最低8位,所以 <code class="docutils literal notranslate"><span class="pre">\x123456</span></code> 等同于 <code class="docutils literal notranslate"><span class="pre">\x56</span></code>。</p>
|
||||
<p><a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> 和 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 异常现在有了更友好的错误消息,其文本内容类似于 <code class="docutils literal notranslate"><span class="pre">'Spam'</span> <span class="pre">instance</span> <span class="pre">has</span> <span class="pre">no</span> <span class="pre">attribute</span> <span class="pre">'eggs'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">name</span> <span class="pre">'eggs'</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">defined</span></code>。 之前的错误消息只是缺少的属性名称,如 <code class="docutils literal notranslate"><span class="pre">eggs</span></code>,因此利用这一事实编写的代码在 2.0 中会中断。</p>
|
||||
<p>Some work has been done to make integers and long integers a bit more
|
||||
interchangeable. In 1.5.2, large-file support was added for Solaris, to allow
|
||||
reading files larger than 2 GiB; this made the <code class="xref py py-meth docutils literal notranslate"><span class="pre">tell()</span></code> method of file
|
||||
objects return a long integer instead of a regular integer. Some code would
|
||||
subtract two file offsets and attempt to use the result to multiply a sequence
|
||||
or slice a string, but this raised a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. In 2.0, long integers
|
||||
can be used to multiply or slice a sequence, and it'll behave as you'd
|
||||
intuitively expect it to; <code class="docutils literal notranslate"><span class="pre">3L</span> <span class="pre">*</span> <span class="pre">'abc'</span></code> produces 'abcabcabc', and
|
||||
<code class="docutils literal notranslate"><span class="pre">(0,1,2,3)[2L:4L]</span></code> produces (2,3). Long integers can also be used in various
|
||||
contexts where previously only integers were accepted, such as in the
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code> method of file objects, and in the formats supported by the <code class="docutils literal notranslate"><span class="pre">%</span></code>
|
||||
operator (<code class="docutils literal notranslate"><span class="pre">%d</span></code>, <code class="docutils literal notranslate"><span class="pre">%i</span></code>, <code class="docutils literal notranslate"><span class="pre">%x</span></code>, etc.). For example, <code class="docutils literal notranslate"><span class="pre">"%d"</span> <span class="pre">%</span> <span class="pre">2L**64</span></code> will
|
||||
produce the string <code class="docutils literal notranslate"><span class="pre">18446744073709551616</span></code>.</p>
|
||||
<p>最微妙的长整数变化是,长整数的 <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> 表示不再有尾随的 'L' 字符,尽管 <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> 表示仍然包含它。许多人在打印长整数时不希望看到 'L' 字符,因为他们不得不专门去掉这个字符。在2.0中,这不再是一个问题,但那些使用 <code class="docutils literal notranslate"><span class="pre">str(longval)[:-1]</span></code> 并假设存在 'L' 的代码,现在将丢失最后一个数字。</p>
|
||||
<p>Taking the <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> of a float now uses a different formatting precision
|
||||
than <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>. <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> uses <code class="docutils literal notranslate"><span class="pre">%.17g</span></code> format string for C's
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sprintf()</span></code>, while <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> uses <code class="docutils literal notranslate"><span class="pre">%.12g</span></code> as before. The effect is that
|
||||
<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> may occasionally show more decimal places than <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>, for
|
||||
certain numbers. For example, the number 8.1 can't be represented exactly in
|
||||
binary, so <code class="docutils literal notranslate"><span class="pre">repr(8.1)</span></code> is <code class="docutils literal notranslate"><span class="pre">'8.0999999999999996'</span></code>, while str(8.1) is
|
||||
<code class="docutils literal notranslate"><span class="pre">'8.1'</span></code>.</p>
|
||||
<p>The <code class="docutils literal notranslate"><span class="pre">-X</span></code> command-line option, which turned all standard exceptions into
|
||||
strings instead of classes, has been removed; the standard exceptions will now
|
||||
always be classes. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">exceptions</span></code> module containing the standard
|
||||
exceptions was translated from Python to a built-in C module, written by Barry
|
||||
Warsaw and Fredrik Lundh.</p>
|
||||
</section>
|
||||
<section id="extending-embedding-changes">
|
||||
<h2>扩展/嵌入更改<a class="headerlink" href="#extending-embedding-changes" title="永久链接至标题">¶</a></h2>
|
||||
<p>有些更改是在底层进行的,仅对编写 C 扩展模块或在更大的应用中嵌入 Python 解释器的人有价值。 如果你不处理 Python 的 C API,可以安全地跳过这一节。</p>
|
||||
<p>The version number of the Python C API was incremented, so C extensions compiled
|
||||
for 1.5.2 must be recompiled in order to work with 2.0. On Windows, it's not
|
||||
possible for Python 2.0 to import a third party extension built for Python 1.5.x
|
||||
due to how Windows DLLs work, so Python will raise an exception and the import
|
||||
will fail.</p>
|
||||
<p>使用 Jim Fulton 的 ExtensionClass 模块的用户将很高兴地发现,已经添加了钩子以支持 ExtensionClasses,因此现在支持 <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a>。 这意味着你不再需要记住编写类似 <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">type(obj)</span> <span class="pre">==</span> <span class="pre">myExtensionClass</span></code> 这样的代码,而可以使用更自然的 <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">isinstance(obj,</span> <span class="pre">myExtensionClass)</span></code>。</p>
|
||||
<p><code class="file docutils literal notranslate"><span class="pre">Python/importdl.c</span></code> 文件,它充满了用于支持在许多不同平台上动态加载的 #ifdef,已被 Greg Stein 清理和重组。现在 <code class="file docutils literal notranslate"><span class="pre">importdl.c</span></code> 非常小,平台特定的代码已被移入一组特定的 <code class="file docutils literal notranslate"><span class="pre">Python/dynload_*.c</span></code> 文件中。 另一个清理工作是:Include/ 目录中有许多包含各种可移植性修改的 <code class="file docutils literal notranslate"><span class="pre">my*.h</span></code> 文件;它们已被合并到一个文件中,即 <code class="file docutils literal notranslate"><span class="pre">Include/pyport.h</span></code>。</p>
|
||||
<p>Vladimir Marangozov's long-awaited malloc restructuring was completed, to make
|
||||
it easy to have the Python interpreter use a custom allocator instead of C's
|
||||
standard <code class="xref py py-func docutils literal notranslate"><span class="pre">malloc()</span></code>. For documentation, read the comments in
|
||||
<code class="file docutils literal notranslate"><span class="pre">Include/pymem.h</span></code> and <code class="file docutils literal notranslate"><span class="pre">Include/objimpl.h</span></code>. For the lengthy
|
||||
discussions during which the interface was hammered out, see the Web archives of
|
||||
the 'patches' and 'python-dev' lists at python.org.</p>
|
||||
<p>最新版本的 MacOS GUSI 开发环境支持 POSIX 线程。 因此,现在 Python 的 POSIX 线程支持在 Macintosh 上也可以使用。 还贡献了使用用户空间 GNU <code class="docutils literal notranslate"><span class="pre">pth</span></code> 库的线程支持。</p>
|
||||
<p>Windows 上的线程支持也得到了增强。 Windows 支持的线程锁在发生争用时才使用内核对象;在常见的没有争用的情况下,他们使用简单得多的函数,这些函数快一个数量级。Python 1.5.2 在 NT 上的线程版本比无线程版本慢两倍;有了 2.0 的改进,差异仅为 10%。 这些改进由 Yakov Markovitch 提供。</p>
|
||||
<p>Python 2.0 的源代码目前只用 ANSI C 原型,所以现在编译 Python 需要一个 ANSI C 的编译器,而不能通过仅使用支持 K&R C 的编译器完成。</p>
|
||||
<p>Previously the Python virtual machine used 16-bit numbers in its bytecode,
|
||||
limiting the size of source files. In particular, this affected the maximum
|
||||
size of literal lists and dictionaries in Python source; occasionally people who
|
||||
are generating Python code would run into this limit. A patch by Charles G.
|
||||
Waldman raises the limit from <code class="docutils literal notranslate"><span class="pre">2^16</span></code> to <code class="docutils literal notranslate"><span class="pre">2^{32}</span></code>.</p>
|
||||
<p>Three new convenience functions intended for adding constants to a module's
|
||||
dictionary at module initialization time were added: <code class="xref py py-func docutils literal notranslate"><span class="pre">PyModule_AddObject()</span></code>,
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">PyModule_AddIntConstant()</span></code>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">PyModule_AddStringConstant()</span></code>. Each
|
||||
of these functions takes a module object, a null-terminated C string containing
|
||||
the name to be added, and a third argument for the value to be assigned to the
|
||||
name. This third argument is, respectively, a Python object, a C long, or a C
|
||||
string.</p>
|
||||
<p>A wrapper API was added for Unix-style signal handlers. <code class="xref py py-func docutils literal notranslate"><span class="pre">PyOS_getsig()</span></code> gets
|
||||
a signal handler and <code class="xref py py-func docutils literal notranslate"><span class="pre">PyOS_setsig()</span></code> will set a new handler.</p>
|
||||
</section>
|
||||
<section id="distutils-making-modules-easy-to-install">
|
||||
<h2>Distutils:使模块易于安装<a class="headerlink" href="#distutils-making-modules-easy-to-install" title="永久链接至标题">¶</a></h2>
|
||||
<p>在 Python 2.0 之前,安装模块是一件繁琐的事情 —— 没有办法自动确定 Python 的安装位置,或者用于扩展模块的编译器选项。 软件作者不得不经历一套繁琐的程序来编辑 Makefile 和配置文件,这些只在 Unix 上真正有效,而 Windows 和 Mac OS 不受支持。 Python 用户面对不同扩展包之间大相径庭的安装说明,这使得管理 Python 成了一件麻烦事。</p>
|
||||
<p>The SIG for distribution utilities, shepherded by Greg Ward, has created the
|
||||
Distutils, a system to make package installation much easier. They form the
|
||||
<a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a> package, a new part of Python's standard library. In the best
|
||||
case, installing a Python module from source will require the same steps: first
|
||||
you simply mean unpack the tarball or zip archive, and the run "<code class="docutils literal notranslate"><span class="pre">python</span>
|
||||
<span class="pre">setup.py</span> <span class="pre">install</span></code>". The platform will be automatically detected, the compiler
|
||||
will be recognized, C extension modules will be compiled, and the distribution
|
||||
installed into the proper directory. Optional command-line arguments provide
|
||||
more control over the installation process, the distutils package offers many
|
||||
places to override defaults -- separating the build from the install, building
|
||||
or installing in non-default directories, and more.</p>
|
||||
<p>为了使用 Distutils,你需要编写一个 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> 脚本。 在简单场景下,当软件仅包含 .py 文件时,最小化的 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> 可以只有几行代码:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
|
||||
<span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">"foo"</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">"1.0"</span><span class="p">,</span>
|
||||
<span class="n">py_modules</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"module1"</span><span class="p">,</span> <span class="s2">"module2"</span><span class="p">])</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>如果软件是由几个包组成的 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> 文件也不会太过复杂:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
|
||||
<span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">"foo"</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">"1.0"</span><span class="p">,</span>
|
||||
<span class="n">packages</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"package"</span><span class="p">,</span> <span class="s2">"package.subpackage"</span><span class="p">])</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>最复杂的情况可能是 C 扩展;下面是一个来自 PyXML 包的示例:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span><span class="p">,</span> <span class="n">Extension</span>
|
||||
|
||||
<span class="n">expat_extension</span> <span class="o">=</span> <span class="n">Extension</span><span class="p">(</span><span class="s1">'xml.parsers.pyexpat'</span><span class="p">,</span>
|
||||
<span class="n">define_macros</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">'XML_NS'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)],</span>
|
||||
<span class="n">include_dirs</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">'extensions/expat/xmltok'</span><span class="p">,</span>
|
||||
<span class="s1">'extensions/expat/xmlparse'</span> <span class="p">],</span>
|
||||
<span class="n">sources</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">'extensions/pyexpat.c'</span><span class="p">,</span>
|
||||
<span class="s1">'extensions/expat/xmltok/xmltok.c'</span><span class="p">,</span>
|
||||
<span class="s1">'extensions/expat/xmltok/xmlrole.c'</span><span class="p">,</span> <span class="p">]</span>
|
||||
<span class="p">)</span>
|
||||
<span class="n">setup</span> <span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s2">"PyXML"</span><span class="p">,</span> <span class="n">version</span> <span class="o">=</span> <span class="s2">"0.5.4"</span><span class="p">,</span>
|
||||
<span class="n">ext_modules</span> <span class="o">=</span><span class="p">[</span> <span class="n">expat_extension</span> <span class="p">]</span> <span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Distutils 还可以负责创建源代码和二进制分发包。运行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">sdist</span></code> 的 "sdist" 命令构建一个源代码分发包,如 <code class="file docutils literal notranslate"><span class="pre">foo-1.0.tar.gz</span></code>。添加新命令并不困难,已经有 "bdist_rpm" 和 "bdist_wininst" 命令,分别用于创建软件的 RPM 分发包和 Windows 安装程序。创建其他分发格式的命令,如 Debian 包和 Solaris <code class="file docutils literal notranslate"><span class="pre">.pkg</span></code> 文件,也在开发的不同阶段。</p>
|
||||
<p>所有这些都记录在一个新手册中,<em>Distributing Python Modules</em>,它加入了Python文档的基本集合中。</p>
|
||||
</section>
|
||||
<section id="xml-modules">
|
||||
<h2>XML 模块<a class="headerlink" href="#xml-modules" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 1.5.2 included a simple XML parser in the form of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>
|
||||
module, contributed by Sjoerd Mullender. Since 1.5.2's release, two different
|
||||
interfaces for processing XML have become common: SAX2 (version 2 of the Simple
|
||||
API for XML) provides an event-driven interface with some similarities to
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>, and the DOM (Document Object Model) provides a tree-based
|
||||
interface, transforming an XML document into a tree of nodes that can be
|
||||
traversed and modified. Python 2.0 includes a SAX2 interface and a stripped-down
|
||||
DOM interface as part of the <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package. Here we will give a brief
|
||||
overview of these new interfaces; consult the Python documentation or the source
|
||||
code for complete details. The Python XML SIG is also working on improved
|
||||
documentation.</p>
|
||||
<section id="sax2-support">
|
||||
<h3>SAX2 支持<a class="headerlink" href="#sax2-support" title="永久链接至标题">¶</a></h3>
|
||||
<p>SAX defines an event-driven interface for parsing XML. To use SAX, you must
|
||||
write a SAX handler class. Handler classes inherit from various classes
|
||||
provided by SAX, and override various methods that will then be called by the
|
||||
XML parser. For example, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">startElement()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">endElement()</span></code>
|
||||
methods are called for every starting and end tag encountered by the parser, the
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">characters()</span></code> method is called for every chunk of character data, and so
|
||||
forth.</p>
|
||||
<p>事件驱动方法的优点是整个文档不必同时驻留在内存中,这在处理非常大的文档时尤其重要。然而,如果你试图以某种复杂的方式修改文档结构,编写 SAX 处理程序类可能会变得非常复杂。</p>
|
||||
<p>例如,这个小示例程序定义了一个处理器,它为每个开始和结束标签打印一条消息,然后使用它来解析文件 <code class="file docutils literal notranslate"><span class="pre">hamlet.xml</span></code>:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml</span> <span class="kn">import</span> <span class="n">sax</span>
|
||||
|
||||
<span class="k">class</span> <span class="nc">SimpleHandler</span><span class="p">(</span><span class="n">sax</span><span class="o">.</span><span class="n">ContentHandler</span><span class="p">):</span>
|
||||
<span class="k">def</span> <span class="nf">startElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="p">):</span>
|
||||
<span class="nb">print</span> <span class="s1">'Start of element:'</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">attrs</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">endElement</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
|
||||
<span class="nb">print</span> <span class="s1">'End of element:'</span><span class="p">,</span> <span class="n">name</span>
|
||||
|
||||
<span class="c1"># Create a parser object</span>
|
||||
<span class="n">parser</span> <span class="o">=</span> <span class="n">sax</span><span class="o">.</span><span class="n">make_parser</span><span class="p">()</span>
|
||||
|
||||
<span class="c1"># Tell it what handler to use</span>
|
||||
<span class="n">handler</span> <span class="o">=</span> <span class="n">SimpleHandler</span><span class="p">()</span>
|
||||
<span class="n">parser</span><span class="o">.</span><span class="n">setContentHandler</span><span class="p">(</span> <span class="n">handler</span> <span class="p">)</span>
|
||||
|
||||
<span class="c1"># Parse a file!</span>
|
||||
<span class="n">parser</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span> <span class="s1">'hamlet.xml'</span> <span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>For more information, consult the Python documentation, or the XML HOWTO at
|
||||
<a class="reference external" href="http://pyxml.sourceforge.net/topics/howto/xml-howto.html">http://pyxml.sourceforge.net/topics/howto/xml-howto.html</a>.</p>
|
||||
</section>
|
||||
<section id="dom-support">
|
||||
<h3>DOM 支持<a class="headerlink" href="#dom-support" title="永久链接至标题">¶</a></h3>
|
||||
<p>The Document Object Model is a tree-based representation for an XML document. A
|
||||
top-level <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code> instance is the root of the tree, and has a single
|
||||
child which is the top-level <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> instance. This <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code>
|
||||
has children nodes representing character data and any sub-elements, which may
|
||||
have further children of their own, and so forth. Using the DOM you can
|
||||
traverse the resulting tree any way you like, access element and attribute
|
||||
values, insert and delete nodes, and convert the tree back into XML.</p>
|
||||
<p>DOM 在修改 XML 文档方面非常有用,因为你可以创建一个 DOM 树,通过添加新节点或重新排列子树来修改它,然后生成一个新的 XML 文档作为输出。你还可以手动构建一个 DOM 树并将其转换为 XML,这比简单地将``<tag1>``...``</tag1>``写入文件更灵活。</p>
|
||||
<p>The DOM implementation included with Python lives in the <a class="reference internal" href="../library/xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code></a>
|
||||
module. It's a lightweight implementation of the Level 1 DOM with support for
|
||||
XML namespaces. The <code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">parseString()</span></code> convenience
|
||||
functions are provided for generating a DOM tree:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">minidom</span>
|
||||
<span class="n">doc</span> <span class="o">=</span> <span class="n">minidom</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">'hamlet.xml'</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p><code class="docutils literal notranslate"><span class="pre">doc</span></code> is a <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code> instance. <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code>, like all the other
|
||||
DOM classes such as <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Text</span></code>, is a subclass of the
|
||||
<code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code> base class. All the nodes in a DOM tree therefore support certain
|
||||
common methods, such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">toxml()</span></code> which returns a string containing the XML
|
||||
representation of the node and its children. Each class also has special
|
||||
methods of its own; for example, <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">Document</span></code>
|
||||
instances have a method to find all child elements with a given tag name.
|
||||
Continuing from the previous 2-line example:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">perslist</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span> <span class="s1">'PERSONA'</span> <span class="p">)</span>
|
||||
<span class="nb">print</span> <span class="n">perslist</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
|
||||
<span class="nb">print</span> <span class="n">perslist</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">toxml</span><span class="p">()</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>对于 <em>Hamlet</em> XML 文件,上面几行代码输出:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="n">PERSONA</span><span class="o">></span><span class="n">CLAUDIUS</span><span class="p">,</span> <span class="n">king</span> <span class="n">of</span> <span class="n">Denmark</span><span class="o">.</span> <span class="o"></</span><span class="n">PERSONA</span><span class="o">></span>
|
||||
<span class="o"><</span><span class="n">PERSONA</span><span class="o">></span><span class="n">HAMLET</span><span class="p">,</span> <span class="n">son</span> <span class="n">to</span> <span class="n">the</span> <span class="n">late</span><span class="p">,</span> <span class="ow">and</span> <span class="n">nephew</span> <span class="n">to</span> <span class="n">the</span> <span class="n">present</span> <span class="n">king</span><span class="o">.</</span><span class="n">PERSONA</span><span class="o">></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>文件的根元素可以通过 <code class="docutils literal notranslate"><span class="pre">doc.documentElement</span></code> 访问,并且可以通过删除、添加或一处节点来听松修改节点来轻松修改其子元素:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">root</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">documentElement</span>
|
||||
|
||||
<span class="c1"># Remove the first child</span>
|
||||
<span class="n">root</span><span class="o">.</span><span class="n">removeChild</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span>
|
||||
|
||||
<span class="c1"># Move the new first child to the end</span>
|
||||
<span class="n">root</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="p">)</span>
|
||||
|
||||
<span class="c1"># Insert the new first child (originally,</span>
|
||||
<span class="c1"># the third child) before the 20th child.</span>
|
||||
<span class="n">root</span><span class="o">.</span><span class="n">insertBefore</span><span class="p">(</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">root</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">20</span><span class="p">]</span> <span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>Again, I will refer you to the Python documentation for a complete listing of
|
||||
the different <code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code> classes and their various methods.</p>
|
||||
</section>
|
||||
<section id="relationship-to-pyxml">
|
||||
<h3>与 PyXML 的关系<a class="headerlink" href="#relationship-to-pyxml" title="永久链接至标题">¶</a></h3>
|
||||
<p>The XML Special Interest Group has been working on XML-related Python code for a
|
||||
while. Its code distribution, called PyXML, is available from the SIG's Web
|
||||
pages at <a class="reference external" href="https://www.python.org/community/sigs/current/xml-sig">https://www.python.org/community/sigs/current/xml-sig</a>. The PyXML distribution also used
|
||||
the package name <code class="docutils literal notranslate"><span class="pre">xml</span></code>. If you've written programs that used PyXML, you're
|
||||
probably wondering about its compatibility with the 2.0 <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package.</p>
|
||||
<p>答案是 Python 2.0 的 <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> 包与 PyXML 不兼容,但可以通过安装最新版本的 PyXML 来使其兼容。 许多应用程序可以依赖 Python 2.0 中包含的 XML 支持,但更复杂的应用程序需要安装完整的 PyXML 包。 安装后,版本为 0.6.0 或更高的 PyXML 将替换随 Python 一起发布的 <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> 包,并且将是标准包的严格超集,添加许多额外的功能。 PyXML 中的一些附加功能包括:</p>
|
||||
<ul class="simple">
|
||||
<li><p>4DOM,一个来自 FourThought, Inc. 的完整 DOM 实现。</p></li>
|
||||
<li><p>xmlproc 验证解析器,由 Lars Marius Garshol 编写。</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">sgmlop</span></code> 解析器加速器模块,由 Fredrik Lundh 编写。</p></li>
|
||||
</ul>
|
||||
</section>
|
||||
</section>
|
||||
<section id="module-changes">
|
||||
<h2>模块更改<a class="headerlink" href="#module-changes" title="永久链接至标题">¶</a></h2>
|
||||
<p>Lots of improvements and bugfixes were made to Python's extensive standard
|
||||
library; some of the affected modules include <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>,
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code>, <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cgi</span></code></a>, <a class="reference internal" href="../library/calendar.html#module-calendar" title="calendar: Functions for working with calendars, including some emulation of the Unix cal program."><code class="xref py py-mod docutils literal notranslate"><span class="pre">calendar</span></code></a>, <a class="reference internal" href="../library/posix.html#module-posix" title="posix: The most common POSIX system calls (normally used via module os). (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">posix</span></code></a>, <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>,
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">xmllib</span></code>, <a class="reference internal" href="../library/aifc.html#module-aifc" title="aifc: Read and write audio files in AIFF or AIFC format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">aifc</span></code></a>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">chunk,</span> <span class="pre">wave</span></code>, <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>, <a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a>,
|
||||
and <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a>. Consult the CVS logs for the exact patch-by-patch details.</p>
|
||||
<p>Brian Gallew contributed OpenSSL support for the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module. OpenSSL
|
||||
is an implementation of the Secure Socket Layer, which encrypts the data being
|
||||
sent over a socket. When compiling Python, you can edit <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code>
|
||||
to include SSL support, which adds an additional function to the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>
|
||||
module: <code class="docutils literal notranslate"><span class="pre">socket.ssl(socket,</span> <span class="pre">keyfile,</span> <span class="pre">certfile)</span></code>, which takes a socket
|
||||
object and returns an SSL socket. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> and <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> modules
|
||||
were also changed to support <code class="docutils literal notranslate"><span class="pre">https://</span></code> URLs, though no one has implemented
|
||||
FTP or SMTP over SSL.</p>
|
||||
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> module has been rewritten by Greg Stein to support HTTP/1.1.
|
||||
Backward compatibility with the 1.5 version of <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> is provided,
|
||||
though using HTTP/1.1 features such as pipelining will require rewriting code to
|
||||
use a different set of interfaces.</p>
|
||||
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">Tkinter</span></code> module now supports Tcl/Tk version 8.1, 8.2, or 8.3, and
|
||||
support for the older 7.x versions has been dropped. The Tkinter module now
|
||||
supports displaying Unicode strings in Tk widgets. Also, Fredrik Lundh
|
||||
contributed an optimization which makes operations like <code class="docutils literal notranslate"><span class="pre">create_line</span></code> and
|
||||
<code class="docutils literal notranslate"><span class="pre">create_polygon</span></code> much faster, especially when using lots of coordinates.</p>
|
||||
<p><a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> 模块在 Oliver Andrich 增强版本的基础上得到了极大的扩展,提供了许多来自 ncurses 和 SYSV curses 的附加功能,如颜色、替代字符集支持、手写板和鼠标支持等。 这意味着该模块不再兼容仅有 BSD curses 的操作系统,但目前似乎没有任何属于这一类别的操作系统正在维护中。</p>
|
||||
<p>如前面讨论的 Python 2.0 对 Unicode 的支持所提到的,<a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> 模块提供的正则表达式的底层实现已经更改。 SRE,一个新的正则表达式引擎,由 Ferdrik Lundh 编写,部分由惠普资助,支持匹配 8 位字符串和 Unicode 字符串。</p>
|
||||
</section>
|
||||
<section id="new-modules">
|
||||
<h2>新增模块<a class="headerlink" href="#new-modules" title="永久链接至标题">¶</a></h2>
|
||||
<p>添加了许多新模块,我们将简单列出它们并附上简要概述;有关特定模块的详细信息,请查阅 2.0 文档。</p>
|
||||
<ul class="simple">
|
||||
<li><p><a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> 模块用于注册在 Python 解释器退出之前调用的函数。当前直接设置 <code class="docutils literal notranslate"><span class="pre">sys.exitfunc</span></code> 的代码应改为使用 <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> 模块,导入 <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> 并使用 <a class="reference internal" href="../library/atexit.html#atexit.register" title="atexit.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">atexit.register()</span></code></a> 调用要在退出时调用的函数。 (由 Skip Montanaro 贡献。)</p></li>
|
||||
<li><p><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>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings</span></code>, <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a>: Added as part of the new
|
||||
Unicode support.</p></li>
|
||||
<li><p><a class="reference internal" href="../library/filecmp.html#module-filecmp" title="filecmp: Compare files efficiently."><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code></a>: Supersedes the old <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmpcache</span></code> and
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">dircmp</span></code> modules, which have now become deprecated. (Contributed by Gordon
|
||||
MacMillan and Moshe Zadka.)</p></li>
|
||||
<li><p><a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a>: 这个模块通过提供 GNU gettext 消息目录库的接口,为 Python 程序提供国际化(I18N)和本地化(L10N)支持。 (由 Barry Warsaw 整合,来自 Martin von Löwis, Peter Funk 和 James Henstridge 的独立贡献。)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">linuxaudiodev</span></code>: Support for the <code class="file docutils literal notranslate"><span class="pre">/dev/audio</span></code> device on Linux, a
|
||||
twin to the existing <code class="xref py py-mod docutils literal notranslate"><span class="pre">sunaudiodev</span></code> module. (Contributed by Peter Bosch,
|
||||
with fixes by Jeremy Hylton.)</p></li>
|
||||
<li><p><a class="reference internal" href="../library/mmap.html#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code></a>: 提供对内存映射文件的接口,支持 Windows 和 Unix。文件的内容可以直接映射到内存中,此时它表现得像一个可变字符串,因此可以读取和修改其内容。它们甚至可以传递给期望普通字符串的函数,例如 <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> 模块。(由 Sam Rushing 贡献,并由 A.M. Kuchling 提供一些扩展。)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code>: An interface to the Expat XML parser. (Contributed by Paul
|
||||
Prescod.)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">robotparser</span></code>: Parse a <code class="file docutils literal notranslate"><span class="pre">robots.txt</span></code> file, which is used for writing
|
||||
Web spiders that politely avoid certain areas of a Web site. The parser accepts
|
||||
the contents of a <code class="file docutils literal notranslate"><span class="pre">robots.txt</span></code> file, builds a set of rules from it, and
|
||||
can then answer questions about the fetchability of a given URL. (Contributed
|
||||
by Skip Montanaro.)</p></li>
|
||||
<li><p><a class="reference internal" href="../library/tabnanny.html#module-tabnanny" title="tabnanny: Tool for detecting white space related problems in Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tabnanny</span></code></a>: 一个模块/脚本,用于检查 Python 源代码中不歧义的缩进。 (由 Tim Peters 贡献。)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">UserString</span></code>: A base class useful for deriving objects that behave like
|
||||
strings.</p></li>
|
||||
<li><p><a class="reference internal" href="../library/webbrowser.html#module-webbrowser" title="webbrowser: Easy-to-use controller for Web browsers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code></a> 模块提供了一种平台独立的方式来在特定 URL 上启动 web 浏览器。对于每个平台,模块会按特定顺序尝试各种浏览器。用户可以通过设置 <em>BROWSER</em> 环境变量来更改启动的浏览器。(最初的灵感来自 Eric S. Raymond 对 <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> 的补丁,该补丁增加了类似的功能,但最终的模块来自 Fred Drake 最初实现的代码 <code class="file docutils literal notranslate"><span class="pre">Tools/idle/BrowserControl.py</span></code>,并由 Fred 适配到标准库中。)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code>: An interface to the Windows registry. <code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code> is an
|
||||
adaptation of functions that have been part of PythonWin since 1995, but has now
|
||||
been added to the core distribution, and enhanced to support Unicode.
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code> was written by Bill Tutt and Mark Hammond.</p></li>
|
||||
<li><p><a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> 模块:用于读取和写入 ZIP 格式的归档文件。 这些归档文件是由 DOS/Windows 上的 <strong class="program">PKZIP</strong> 或 Unix 上的 <strong class="program">zip</strong> 生成的,不要与 <strong class="program">gzip</strong> 格式文件(由 <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> 支持)混淆。 (由 James C. Ahlstrom 贡献。)</p></li>
|
||||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">imputil</span></code>: A module that provides a simpler way for writing customized
|
||||
import hooks, in comparison to the existing <code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> module. (Implemented
|
||||
by Greg Stein, with much discussion on python-dev along the way.)</p></li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="idle-improvements">
|
||||
<h2>IDLE 改进<a class="headerlink" href="#idle-improvements" title="永久链接至标题">¶</a></h2>
|
||||
<p>IDLE 是官方的 Python 跨平台 IDE,使用 Tkinter 编写。 Python 2.0 包括了 IDLE 0.6,它增加了许多新特性和改进。 部分新内容列表:</p>
|
||||
<ul class="simple">
|
||||
<li><p>界面改进和优化,特别是在语法高亮和自动缩进方面。</p></li>
|
||||
<li><p>类浏览器现在将显示更多信息,例如一个模块中最高层级的函数。</p></li>
|
||||
<li><p>制表符宽度现在是一个用户可设置的选项。在打开现有的 Python 文件时,IDLE 会自动检测缩进约定并进行适应。</p></li>
|
||||
<li><p>现在支持在各种平台上调用浏览器,用于在浏览器中打开 Python 文档。</p></li>
|
||||
<li><p>IDLE 现在有一个命令行,它与原版 Python 解释器大致相同。</p></li>
|
||||
<li><p>在许多地方添加了调用提示。</p></li>
|
||||
<li><p>IDLE 现在可以作为一个包被安装。</p></li>
|
||||
<li><p>在编辑器窗口中,目前在底部位置增加了一个行/列显示栏。</p></li>
|
||||
<li><p>三个新的键盘快捷键:检查模块 (<kbd class="kbd docutils literal notranslate">Alt-F5</kbd>),导入模块 (<kbd class="kbd docutils literal notranslate">F5</kbd>) 和执行脚本 (<kbd class="kbd docutils literal notranslate">Ctrl-F5</kbd>)。</p></li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="deleted-and-deprecated-modules">
|
||||
<h2>删除和弃用的模块<a class="headerlink" href="#deleted-and-deprecated-modules" title="永久链接至标题">¶</a></h2>
|
||||
<p>A few modules have been dropped because they're obsolete, or because there are
|
||||
now better ways to do the same thing. The <code class="xref py py-mod docutils literal notranslate"><span class="pre">stdwin</span></code> module is gone; it was
|
||||
for a platform-independent windowing toolkit that's no longer developed.</p>
|
||||
<p>多个模块已被移至 <code class="file docutils literal notranslate"><span class="pre">lib-old</span></code> 子目录: <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmpcache</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dircmp</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dump</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">find</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">grep</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">packmail</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">poly</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">util</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">whatsound</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">zmod</span></code>。 如果你还有依赖于被移至 <code class="file docutils literal notranslate"><span class="pre">lib-old</span></code> 的模块的代码,你可以简单地将该目录添加到 <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> 来恢复它们,但建议你对任何使用这些模块的代码进行更新。</p>
|
||||
</section>
|
||||
<section id="acknowledgements">
|
||||
<h2>致谢<a class="headerlink" href="#acknowledgements" title="永久链接至标题">¶</a></h2>
|
||||
<p>作者感谢以下人士对本文的各种草稿提出建议: David Bolen, Mark Hammond, Gregg Hauser, Jeremy Hylton, Fredrik Lundh, Detlef Lannert, Aahz Maruch, Skip Montanaro, Vladimir Marangozov, Tobias Polzin, Guido van Rossum, Neil Schemenauer, and Russ Schmidt.</p>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h3><a href="../contents.html">目录</a></h3>
|
||||
<ul>
|
||||
<li><a class="reference internal" href="#">Python 2.0 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#introduction">概述</a></li>
|
||||
<li><a class="reference internal" href="#what-about-python-1-6">Python 1.6 将会怎样?</a></li>
|
||||
<li><a class="reference internal" href="#new-development-process">新开发流程</a></li>
|
||||
<li><a class="reference internal" href="#unicode">Unicode</a></li>
|
||||
<li><a class="reference internal" href="#list-comprehensions">列表推导式</a></li>
|
||||
<li><a class="reference internal" href="#augmented-assignment">增强赋值</a></li>
|
||||
<li><a class="reference internal" href="#string-methods">字符串的方法</a></li>
|
||||
<li><a class="reference internal" href="#garbage-collection-of-cycles">循环的垃圾回收</a></li>
|
||||
<li><a class="reference internal" href="#other-core-changes">其他核心变化</a><ul>
|
||||
<li><a class="reference internal" href="#minor-language-changes">细微的语言特性修改</a></li>
|
||||
<li><a class="reference internal" href="#changes-to-built-in-functions">对于内置函数的修改</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#porting-to-2-0">移植 Python 2.0</a></li>
|
||||
<li><a class="reference internal" href="#extending-embedding-changes">扩展/嵌入更改</a></li>
|
||||
<li><a class="reference internal" href="#distutils-making-modules-easy-to-install">Distutils:使模块易于安装</a></li>
|
||||
<li><a class="reference internal" href="#xml-modules">XML 模块</a><ul>
|
||||
<li><a class="reference internal" href="#sax2-support">SAX2 支持</a></li>
|
||||
<li><a class="reference internal" href="#dom-support">DOM 支持</a></li>
|
||||
<li><a class="reference internal" href="#relationship-to-pyxml">与 PyXML 的关系</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a class="reference internal" href="#module-changes">模块更改</a></li>
|
||||
<li><a class="reference internal" href="#new-modules">新增模块</a></li>
|
||||
<li><a class="reference internal" href="#idle-improvements">IDLE 改进</a></li>
|
||||
<li><a class="reference internal" href="#deleted-and-deprecated-modules">删除和弃用的模块</a></li>
|
||||
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="2.1.html"
|
||||
title="上一章">Python 2.1 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="changelog.html"
|
||||
title="下一章">更新日志</a></p>
|
||||
<div role="note" aria-label="source link">
|
||||
<h3>本页</h3>
|
||||
<ul class="this-page-menu">
|
||||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||||
<li>
|
||||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.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="changelog.html" title="更新日志"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="2.1.html" title="Python 2.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>
|
||||
782
web/python-docs/whatsnew/2.1.html
Normal file
782
web/python-docs/whatsnew/2.1.html
Normal file
@@ -0,0 +1,782 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||||
<head>
|
||||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||||
|
||||
<title>Python 2.1 有什么新变化 — 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.0 有什么新变化" href="2.0.html" />
|
||||
<link rel="prev" title="Python 2.2 有什么新变化" href="2.2.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/2.1.html" />
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<style>
|
||||
@media only screen {
|
||||
table.full-width-table {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="mobile-nav">
|
||||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||||
<label for="menuToggler" class="toggler__label">
|
||||
<span></span>
|
||||
</label>
|
||||
<nav class="nav-content" role="navigation">
|
||||
<a href="https://www.python.org/" class="nav-logo">
|
||||
<img src="../_static/py.svg" alt="Logo"/>
|
||||
</a>
|
||||
<div class="version_switcher_placeholder"></div>
|
||||
<form role="search" class="search" action="../search.html" method="get">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||||
<path fill-rule="nonzero"
|
||||
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
||||
</svg>
|
||||
<input type="text" name="q" aria-label="快速搜索"/>
|
||||
<input type="submit" value="转向"/>
|
||||
</form>
|
||||
</nav>
|
||||
<div class="menu-wrapper">
|
||||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||||
<div class="language_switcher_placeholder"></div>
|
||||
<h3><a href="../contents.html">目录</a></h3>
|
||||
<ul>
|
||||
<li><a class="reference internal" href="#">Python 2.1 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#introduction">概述</a></li>
|
||||
<li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: 嵌套的作用域</a></li>
|
||||
<li><a class="reference internal" href="#pep-236-future-directives">PEP 236: __future__ 指令</a></li>
|
||||
<li><a class="reference internal" href="#pep-207-rich-comparisons">PEP 207: 富比较</a></li>
|
||||
<li><a class="reference internal" href="#pep-230-warning-framework">PEP 230: 警告框架</a></li>
|
||||
<li><a class="reference internal" href="#pep-229-new-build-system">PEP 229: 新的构建系统</a></li>
|
||||
<li><a class="reference internal" href="#pep-205-weak-references">PEP 205: 弱引用</a></li>
|
||||
<li><a class="reference internal" href="#pep-232-function-attributes">PEP 232: 函数属性</a></li>
|
||||
<li><a class="reference internal" href="#pep-235-importing-modules-on-case-insensitive-platforms">PEP 235: 在大小写不敏感的平台上导入模块</a></li>
|
||||
<li><a class="reference internal" href="#pep-217-interactive-display-hook">PEP 217: 交互模式显示钩子</a></li>
|
||||
<li><a class="reference internal" href="#pep-208-new-coercion-model">PEP 208: 新的强制转换模型</a></li>
|
||||
<li><a class="reference internal" href="#pep-241-metadata-in-python-packages">PEP 241: Python 包中的元数据</a></li>
|
||||
<li><a class="reference internal" href="#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li><a class="reference internal" href="#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="2.2.html"
|
||||
title="上一章">Python 2.2 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="2.0.html"
|
||||
title="下一章">Python 2.0 有什么新变化</a></p>
|
||||
<div role="note" aria-label="source link">
|
||||
<h3>本页</h3>
|
||||
<ul class="this-page-menu">
|
||||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||||
<li>
|
||||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.1.rst"
|
||||
rel="nofollow">显示源代码
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||||
你应当升级版本,并阅读
|
||||
<a href="/3/whatsnew/2.1.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.0.html" title="Python 2.0 有什么新变化"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="2.2.html" title="Python 2.2 有什么新变化"
|
||||
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-2-1">
|
||||
<h1>Python 2.1 有什么新变化<a class="headerlink" href="#what-s-new-in-python-2-1" title="永久链接至标题">¶</a></h1>
|
||||
<dl class="field-list simple">
|
||||
<dt class="field-odd">作者</dt>
|
||||
<dd class="field-odd"><p>A.M. Kuchling</p>
|
||||
</dd>
|
||||
</dl>
|
||||
<section id="introduction">
|
||||
<h2>概述<a class="headerlink" href="#introduction" title="永久链接至标题">¶</a></h2>
|
||||
<p>本文介绍了 Python 2.1 的新增特性。 虽然 2.1 的改变没有 Python 2.0 那么多,但是仍然有一些令人惊喜的东西。 2.1 是第一个使用 Python 增强提议,即 PEP 来进行引导的发行版,因此大部分重要的改变都有相应的 PEP 来提供有关改变的更完整文档和设计思路。 本文并未试图完整记录所有的新特性,而是为 Python 程序员提供新特性的简单概览。 请参阅 Python 2.1 文档,或特定的 PEP,获取针对你感兴趣的任何新特性的更多细节。</p>
|
||||
<p>Python 开发团队的一个近期目标是加速新发行版的步调,使得每 6 到 9 个月就有一个新发行版。 2.1 是基于这个新步调推出的第一个发行版,第一个内测版将于一月发布,即 2.0 最终版发布 3 个月之后。</p>
|
||||
<p>Python 2.1 的最终版本于2001年4月17日发布。</p>
|
||||
</section>
|
||||
<section id="pep-227-nested-scopes">
|
||||
<h2>PEP 227: 嵌套的作用域<a class="headerlink" href="#pep-227-nested-scopes" title="永久链接至标题">¶</a></h2>
|
||||
<p>Python 2.1 中的最大改变是 Python 的作用域规则。 在 Python 2.0 中,任意给定的时刻至多使用三个命名空间来查找变量名称:局部、模块和内置命名空间。 这往往会导致令人吃惊的结果因为它与人们直觉上的预期不相匹配。 例如,一个嵌套的递归函数将不起作用:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
|
||||
<span class="o">...</span>
|
||||
<span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
<span class="k">return</span> <span class="n">g</span><span class="p">(</span><span class="n">value</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">g()</span></code> 将总是引发 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 异常,因为名称 <code class="docutils literal notranslate"><span class="pre">g</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="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> 的代码中你经常能发现局部变量通过作为参数的默认值被拷贝。</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
|
||||
<span class="s2">"Return list of any entries equal to 'name'"</span>
|
||||
<span class="n">L</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">:</span> <span class="n">x</span> <span class="o">==</span> <span class="n">name</span><span class="p">,</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">list_attribute</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">L</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>结果将会严重损害以高度函数式风格编写的 Python 代码的可读性。</p>
|
||||
<p>Python 2.1 最显著的改变是增加了静态作用域这一语言特征来解决此问题。 作为它的第一项影响,在上述示例中的 <code class="docutils literal notranslate"><span class="pre">name=name</span></code> 默认参数现在将不再必要。 简单地说,当一个函数内部的给定变量名没有被赋值时(通过赋值语句,或者 <a class="reference internal" href="../reference/compound_stmts.html#def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">def</span></code></a>, <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> 或 <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> 语句),对该变量的引用将在外层作用域的局部命名空间中查找。 对于该规则的更详细解释,以及具体实现的分析,请参阅相应的 PEP。</p>
|
||||
<p>对于同时在模块层级和包含下层函数定义的函数内部局部变量使用了相同变量名的代码来说这项改变可能会导致一些兼容性问题。 不过这看来不太可能发生,因为阅读这样的代码本来就会相当令人困惑。</p>
|
||||
<p>此项改变的一个附带影响是在特定条件下函数作用域内部 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> 和 <code class="docutils literal notranslate"><span class="pre">exec</span></code> 语句将不允许使用。 Python 参考手册已经写明 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> 仅在模块最高层级上是可用的,但此前 CPython 解释器从未强制实施此规则。 作为嵌套作用域具体实现的一部分,将 Python 源码转为字节码的编译器会生成不同的代码来访问某个包含作用域内的变量。 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> 和 <code class="docutils literal notranslate"><span class="pre">exec</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/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> 异常来提示。</p>
|
||||
<p>为了使前面的解释更清楚,下面是一个例子:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
|
||||
<span class="c1"># The next line is a syntax error</span>
|
||||
<span class="n">exec</span> <span class="s1">'x=2'</span>
|
||||
<span class="k">def</span> <span class="nf">g</span><span class="p">():</span>
|
||||
<span class="k">return</span> <span class="n">x</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>包含 <code class="docutils literal notranslate"><span class="pre">exec</span></code> 语句的第 4 行有语法错误,因为 <code class="docutils literal notranslate"><span class="pre">exec</span></code> 将定义一个名为 <code class="docutils literal notranslate"><span class="pre">x</span></code> 的新局部变量,它的值应当被 <code class="xref py py-func docutils literal notranslate"><span class="pre">g()</span></code> 访问。</p>
|
||||
<p>这应该不会是太大的限制,因为 <code class="docutils literal notranslate"><span class="pre">exec</span></code> 在多数 Python 代码中都极少被使用(而当它被使用时,往往也是个存在糟糕设计的信号)。</p>
|
||||
<p>由于兼容性问题,嵌套作用域被逐步引入;在 Python 2.1 中,它们默认未启用,但可以通过在模块中使用 future 语句来开启,如 <span class="target" id="index-36"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0236"><strong>PEP 236</strong></a> 所述。 (参见下一节对 <span class="target" id="index-37"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0236"><strong>PEP 236</strong></a> 的进一步讨论。) 在 Python 2.2 中,嵌套作用域将成为默认设置,并且无法关闭,但用户将有整个 2.1 版本的生命周期来修复因引入嵌套作用域而导致的任何问题。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-38"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0227"><strong>PEP 227</strong></a> - 静态嵌套作用域</dt><dd><p>由 Jeremy Hylton 撰写并实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-236-future-directives">
|
||||
<h2>PEP 236: __future__ 指令<a class="headerlink" href="#pep-236-future-directives" title="永久链接至标题">¶</a></h2>
|
||||
<p>对嵌套作用域的反应引起了广泛关注,人们担心在 Python 2.1 版本发布时会破坏现有代码,强烈的反应促使 Python 开发者采取了更保守的策略。这个策略包括引入一种约定,在版本 N 中启用可选功能,该功能将在版本 N+1 中成为强制功能。</p>
|
||||
<p>语法使用 <code class="docutils literal notranslate"><span class="pre">from...import</span></code> 语句,使用保留模块名 <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>。可以通过以下语句启用嵌套作用域:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">nested_scopes</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>虽然它看起来像一个普通的 <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> 语句,但实际上并不是;关于此类 future 语句的位置有严格的规定。它们只能放在模块的顶部,必须位于任何 Python 代码或常规 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 语句之前。这是因为这样的语句会影响 Python 字节码编译器解析代码和生成字节码的方式,因此它们必须在任何会生成字节码的语句之前出现。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-39"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0236"><strong>PEP 236</strong></a> - 回到 <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></dt><dd><p>由 Tim Peters 撰写,主要由 Jeremy Hylton 实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-207-rich-comparisons">
|
||||
<h2>PEP 207: 富比较<a class="headerlink" href="#pep-207-rich-comparisons" title="永久链接至标题">¶</a></h2>
|
||||
<p>In earlier versions, Python's support for implementing comparisons on user-defined
|
||||
classes and extension types was quite simple. Classes could implement a
|
||||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method that was given two instances of a class, and could only
|
||||
return 0 if they were equal or +1 or -1 if they weren't; the method couldn't
|
||||
raise an exception or return anything other than a Boolean value. Users of
|
||||
Numeric Python often found this model too weak and restrictive, because in the
|
||||
number-crunching programs that numeric Python is used for, it would be more
|
||||
useful to be able to perform elementwise comparisons of two matrices, returning
|
||||
a matrix containing the results of a given comparison for each element. If the
|
||||
two matrices are of different sizes, then the compare has to be able to raise an
|
||||
exception to signal the error.</p>
|
||||
<p>在 Python 2.1 中增加了富比较操作以支持这一需求。 Python 类现在可以单独重载 <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>, <code class="docutils literal notranslate"><span class="pre">==</span></code> 和 <code class="docutils literal notranslate"><span class="pre">!=</span></code> 中的每个操作。 新的魔术方法名称如下:</p>
|
||||
<table class="docutils align-default">
|
||||
<colgroup>
|
||||
<col style="width: 41%" />
|
||||
<col style="width: 59%" />
|
||||
</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><code class="docutils literal notranslate"><span class="pre"><</span></code></p></td>
|
||||
<td><p><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></p></td>
|
||||
</tr>
|
||||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre"><=</span></code></p></td>
|
||||
<td><p><a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code></a></p></td>
|
||||
</tr>
|
||||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">></span></code></p></td>
|
||||
<td><p><a class="reference internal" href="../reference/datamodel.html#object.__gt__" title="object.__gt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code></a></p></td>
|
||||
</tr>
|
||||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">>=</span></code></p></td>
|
||||
<td><p><a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code></a></p></td>
|
||||
</tr>
|
||||
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">==</span></code></p></td>
|
||||
<td><p><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></p></td>
|
||||
</tr>
|
||||
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">!=</span></code></p></td>
|
||||
<td><p><a class="reference internal" href="../reference/datamodel.html#object.__ne__" title="object.__ne__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ne__()</span></code></a></p></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>(这些魔术方法是以对应的 Fortran 操作符命名的,如 <code class="docutils literal notranslate"><span class="pre">.LT.</span></code>、<code class="docutils literal notranslate"><span class="pre">.LE.</span></code> 等。 数值程序员几乎肯定对这些名称非常熟悉,并且会发现它们易于记忆。)</p>
|
||||
<p>每个这样的魔术方法的形式都是 <code class="docutils literal notranslate"><span class="pre">method(self,</span> <span class="pre">other)</span></code>,其中 <code class="docutils literal notranslate"><span class="pre">self</span></code> 是操作符左侧的对象,而 <code class="docutils literal notranslate"><span class="pre">other</span></code> 是操作符右侧的对象。 例如,表达式 <code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre"><</span> <span class="pre">B</span></code> 会调用 <code class="docutils literal notranslate"><span class="pre">A.__lt__(B)</span></code>。</p>
|
||||
<p>这些魔术方法可以返回任何类型的值:布尔值、矩阵、列表或任何其他 Python 对象。或者,如果比较是不可能的、不一致的或没有意义的,它们也可以引发异常。</p>
|
||||
<p>The built-in <code class="docutils literal notranslate"><span class="pre">cmp(A,B)</span></code> function can use the rich comparison machinery,
|
||||
and now accepts an optional argument specifying which comparison operation to
|
||||
use; this is given as one of the strings <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>,
|
||||
<code class="docutils literal notranslate"><span class="pre">"=="</span></code>, or <code class="docutils literal notranslate"><span class="pre">"!="</span></code>. If called without the optional third argument,
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> will only return -1, 0, or +1 as in previous versions of Python;
|
||||
otherwise it will call the appropriate method and can return any Python object.</p>
|
||||
<p>对于 C 程序员来说,也有相应的变更;类型对象中有一个新的槽位 <code class="docutils literal notranslate"><span class="pre">tp_richcmp</span></code> 以及一个用于执行指定富比较的 API。 这里我不会涉及 C API 的具体内容,完整的相关函数列表请参阅 <span class="target" id="index-40"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0207"><strong>PEP 207</strong></a> 或 2.1 的 C API 文档。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-41"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0207"><strong>PEP 207</strong></a> - 富比较</dt><dd><p>由 Guido van Rossum 编写,大量参考 David Ascher 的先期工作,并由 Guido van Rossum 实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-230-warning-framework">
|
||||
<h2>PEP 230: 警告框架<a class="headerlink" href="#pep-230-warning-framework" title="永久链接至标题">¶</a></h2>
|
||||
<p>在过去的 10 年中,Python 积累了一定数量的过时模块和功能。 由于无法确切知道某个功能被使用的程度:可能没有程序依赖该功能,也可能有很多程序依赖,因此很难确定何时可以安全地移除某个功能,为了以更结构化的方式移除旧功能,添加了一个警告框架。 当 Python 开发者想要废弃某个功能时,它会在下一个 Python 版本中首先触发一个警告。 然后,在随后的Python版本中可以移除该功能,这样用户将有一个完整的发布周期来删除对旧功能的使用。</p>
|
||||
<p>Python 2.1 增加了警告框架以用于此方案。 它增加了一个 <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> 模块,该模块提供了发出警告的函数,以及过滤掉不想显示的警告的功能。 第三方模块也可以使用这个框架来弃用它们不再希望支持的旧功能。</p>
|
||||
<p>例如,在 Python 2.1 中 <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> 模块已被弃用,因此导入它时会打印一条警告信息:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">regex</span>
|
||||
<span class="go">__main__:1: DeprecationWarning: the regex module</span>
|
||||
<span class="go"> is deprecated; please use the re module</span>
|
||||
<span class="gp">>>></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>警告可以通过调用 <a class="reference internal" href="../library/warnings.html#warnings.warn" title="warnings.warn"><code class="xref py py-func docutils literal notranslate"><span class="pre">warnings.warn()</span></code></a> 函数来发出:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">"feature X no longer supported"</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>第一个形参是警告消息;额外的可选形参可被用来指定一个专门的警告类别。</p>
|
||||
<p>Filters can be added to disable certain warnings; a regular expression pattern
|
||||
can be applied to the message or to the module name in order to suppress a
|
||||
warning. For example, you may have a program that uses the <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> module
|
||||
and not want to spare the time to convert it to use the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module right
|
||||
now. The warning can be suppressed by calling</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">warnings</span>
|
||||
<span class="n">warnings</span><span class="o">.</span><span class="n">filterwarnings</span><span class="p">(</span><span class="n">action</span> <span class="o">=</span> <span class="s1">'ignore'</span><span class="p">,</span>
|
||||
<span class="n">message</span><span class="o">=</span><span class="s1">'.*regex module is deprecated'</span><span class="p">,</span>
|
||||
<span class="n">category</span><span class="o">=</span><span class="ne">DeprecationWarning</span><span class="p">,</span>
|
||||
<span class="n">module</span> <span class="o">=</span> <span class="s1">'__main__'</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This adds a filter that will apply only to warnings of the class
|
||||
<a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> triggered in the <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> module, and applies
|
||||
a regular expression to only match the message about the <code class="xref py py-mod docutils literal notranslate"><span class="pre">regex</span></code> module
|
||||
being deprecated, and will cause such warnings to be ignored. Warnings can also
|
||||
be printed only once, printed every time the offending code is executed, or
|
||||
turned into exceptions that will cause the program to stop (unless the
|
||||
exceptions are caught in the usual way, of course).</p>
|
||||
<p>Python 的 C API 也增加了用于发出警告的函数;详情请参阅 PEP 230 或 Python 的 API 文档。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-42"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0005"><strong>PEP 5</strong></a> - 语言演化的准则</dt><dd><p>该文档由 Paul Prescod 撰写,旨在规定移除 Python 旧功能时应遵循的程序。 尽管本文描述的政策尚未被正式采纳,但最终的政策可能不会与 Prescod 的提议有太大不同。</p>
|
||||
</dd>
|
||||
<dt><span class="target" id="index-43"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0230"><strong>PEP 230</strong></a> - 警告框架</dt><dd><p>由 Guido van Rossum 撰写并实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-229-new-build-system">
|
||||
<h2>PEP 229: 新的构建系统<a class="headerlink" href="#pep-229-new-build-system" title="永久链接至标题">¶</a></h2>
|
||||
<p>在编译 Python 时,用户必须进入并编辑 <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 文件以启用各种附加模块;默认集相对较小,并且仅限于在大多数 Unix 平台上编译的模块。这意味着在具有更多功能的 Unix 平台上,特别是 Linux,Python 安装通常不包含所有可能有用的模块。</p>
|
||||
<p>Python 2.0 添加了 Distutils,一组用于分发和安装扩展模块的模块。在 Python 2.1 中,Distutils 被用于编译大部分标准库扩展模块,自动检测当前机器上支持哪些模块。希望这将使 Python 的安装更加容易并具有更多功能。</p>
|
||||
<p>不再需要编辑 <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 文件来启用模块,而是在 Python 源代码分发包的顶层目录运行一个 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> 脚本,该脚本在构建时尝试通过检查系统上的模块和头文件来发现可以启用那些模块。 如果某个模块已在 <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 中配置,则 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> 脚本不会尝试编译该模块,并会遵从 <code class="file docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 文件中的内容。 这提供了一种方式来指定特定平台所需的任何奇怪的命令行旗标或库。</p>
|
||||
<p>在对构建机制的另一项重大更改中,Neil Schemenauer 对其进行了重组,现在 Python 使用单一的非递归 makefile,而不是在顶层目录和 <code class="file docutils literal notranslate"><span class="pre">Python/</span></code>、<code class="file docutils literal notranslate"><span class="pre">Parser/</span></code>、<code class="file docutils literal notranslate"><span class="pre">Objects/`和</span> <span class="pre">:file:`Modules/</span></code> 子目录中的多个 makefile。这使得构建 Python 更快,同时也使修改 Makefile 更加清晰和简单。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-44"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0229"><strong>PEP 229</strong></a> - 使用 Distutils 来构建 Python</dt><dd><p>由 A.M. Kuchling 撰写并实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-205-weak-references">
|
||||
<h2>PEP 205: 弱引用<a class="headerlink" href="#pep-205-weak-references" title="永久链接至标题">¶</a></h2>
|
||||
<p>弱引用,通过 <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> 模块提供,是 Python 程序员工具箱中一种较小但有用的新数据类型。</p>
|
||||
<p>存储一个指向对象的引用(例如,在字典或列表中)会导致该对象永久存活。 在某些特定情况下,这种行为是不符合需要的,最常见的是对象缓存,另一个是像树这样的数据结构中的循环引用。</p>
|
||||
<p>例如,考虑一个记忆化函数,它通过将函数的参数及其结果存储在字典中来缓存另一个函数 <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> 的结果:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>
|
||||
<span class="k">def</span> <span class="nf">memoize</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||
<span class="k">if</span> <span class="n">_cache</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span>
|
||||
|
||||
<span class="n">retval</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Cache the returned object</span>
|
||||
<span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">retval</span>
|
||||
|
||||
<span class="k">return</span> <span class="n">retval</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>This version works for simple things such as integers, but it has a side effect;
|
||||
the <code class="docutils literal notranslate"><span class="pre">_cache</span></code> dictionary holds a reference to the return values, so they'll
|
||||
never be deallocated until the Python process exits and cleans up. This isn't
|
||||
very noticeable for integers, but if <code class="xref py py-func docutils literal notranslate"><span class="pre">f()</span></code> returns an object, or a data
|
||||
structure that takes up a lot of memory, this can be a problem.</p>
|
||||
<p>弱引用提供了一种实现缓存的方法,不会让对象在其生命周期结束后仍然存活。 如果一个对象仅通过弱引用访问,该对象将被释放,并且弱引用将指示它所引用的对象不再存在。 通过调用 <code class="docutils literal notranslate"><span class="pre">wr</span> <span class="pre">=</span> <span class="pre">weakref.ref(obj)</span></code> 来创建对对象 <em>obj</em> 的弱引用。 通过调用弱引用,就像调用函数一样,可以返回被引用的对象: <code class="docutils literal notranslate"><span class="pre">wr()</span></code>。 如果对象仍然存在,它将返回被引用的对象;如果对象不再存在,则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||||
<p>This makes it possible to write a <code class="xref py py-func docutils literal notranslate"><span class="pre">memoize()</span></code> function whose cache doesn't
|
||||
keep objects alive, by storing weak references in the cache.</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>
|
||||
<span class="k">def</span> <span class="nf">memoize</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||
<span class="k">if</span> <span class="n">_cache</span><span class="o">.</span><span class="n">has_key</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||
<span class="n">obj</span> <span class="o">=</span> <span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]()</span>
|
||||
<span class="c1"># If weak reference object still exists,</span>
|
||||
<span class="c1"># return it</span>
|
||||
<span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span> <span class="n">obj</span>
|
||||
|
||||
<span class="n">retval</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
|
||||
|
||||
<span class="c1"># Cache a weak reference</span>
|
||||
<span class="n">_cache</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">retval</span><span class="p">)</span>
|
||||
|
||||
<span class="k">return</span> <span class="n">retval</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module also allows creating proxy objects which behave like
|
||||
weak references --- an object referenced only by proxy objects is deallocated --
|
||||
but instead of requiring an explicit call to retrieve the object, the proxy
|
||||
transparently forwards all operations to the object as long as the object still
|
||||
exists. If the object is deallocated, attempting to use a proxy will cause a
|
||||
<code class="xref py py-exc docutils literal notranslate"><span class="pre">weakref.ReferenceError</span></code> exception to be raised.</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">proxy</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">proxy</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
|
||||
<span class="n">proxy</span><span class="o">.</span><span class="n">attr</span> <span class="c1"># Equivalent to obj.attr</span>
|
||||
<span class="n">proxy</span><span class="o">.</span><span class="n">meth</span><span class="p">()</span> <span class="c1"># Equivalent to obj.meth()</span>
|
||||
<span class="k">del</span> <span class="n">obj</span>
|
||||
<span class="n">proxy</span><span class="o">.</span><span class="n">attr</span> <span class="c1"># raises weakref.ReferenceError</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-45"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0205"><strong>PEP 205</strong></a> - 弱引用</dt><dd><p>由 Fred L. Drake, Jr 撰写并实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-232-function-attributes">
|
||||
<h2>PEP 232: 函数属性<a class="headerlink" href="#pep-232-function-attributes" title="永久链接至标题">¶</a></h2>
|
||||
<p>In Python 2.1, functions can now have arbitrary information attached to them.
|
||||
People were often using docstrings to hold information about functions and
|
||||
methods, because the <code class="docutils literal notranslate"><span class="pre">__doc__</span></code> attribute was the only way of attaching any
|
||||
information to a function. For example, in the Zope Web application server,
|
||||
functions are marked as safe for public access by having a docstring, and in
|
||||
John Aycock's SPARK parsing framework, docstrings hold parts of the BNF grammar
|
||||
to be parsed. This overloading is unfortunate, since docstrings are really
|
||||
intended to hold a function's documentation; for example, it means you can't
|
||||
properly document functions intended for private use in Zope.</p>
|
||||
<p>现在可以使用常规的 Python 语法在函数上设置和检索任意属性:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">pass</span>
|
||||
|
||||
<span class="n">f</span><span class="o">.</span><span class="n">publish</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<span class="n">f</span><span class="o">.</span><span class="n">secure</span> <span class="o">=</span> <span class="mi">1</span>
|
||||
<span class="n">f</span><span class="o">.</span><span class="n">grammar</span> <span class="o">=</span> <span class="s2">"A ::= B (C D)*"</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>The dictionary containing attributes can be accessed as the function's
|
||||
<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>. Unlike the <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> attribute of class instances, in
|
||||
functions you can actually assign a new dictionary to <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>, though
|
||||
the new value is restricted to a regular Python dictionary; you <em>can't</em> be
|
||||
tricky and set it to a <code class="xref py py-class docutils literal notranslate"><span class="pre">UserDict</span></code> instance, or any other random object
|
||||
that behaves like a mapping.</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-46"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0232"><strong>PEP 232</strong></a> - 函数属性</dt><dd><p>由 Barry Warsaw 撰写并实现</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-235-importing-modules-on-case-insensitive-platforms">
|
||||
<h2>PEP 235: 在大小写不敏感的平台上导入模块<a class="headerlink" href="#pep-235-importing-modules-on-case-insensitive-platforms" title="永久链接至标题">¶</a></h2>
|
||||
<p>一些操作系统的文件系统是大小写不敏感的,MacOS 和 Windows 是主要的例子;在这些系统上,无法区分文件名 <code class="docutils literal notranslate"><span class="pre">FILE.PY</span></code> 和 <code class="docutils literal notranslate"><span class="pre">file.py</span></code>,尽管它们确实以原始大小写存储文件名(它们也是保留大小写的)。</p>
|
||||
<p>在 Python 2.1 中,<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> 语句可以在不区分大小写的平台上模拟大小写敏感性。 现在,Python 默认搜索第一个大小写敏感匹配的文件,如果找不到这样的文件,就会引发 <a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a>,因此 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">file</span></code> 不会导入名为 <code class="docutils literal notranslate"><span class="pre">FILE.PY</span></code> 的模块。 在启动 Python 解释器之前,可以通过设置 <span class="target" id="index-47"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONCASEOK"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCASEOK</span></code></a> 环境变量来请求大小写不敏感匹配。</p>
|
||||
</section>
|
||||
<section id="pep-217-interactive-display-hook">
|
||||
<h2>PEP 217: 交互模式显示钩子<a class="headerlink" href="#pep-217-interactive-display-hook" title="永久链接至标题">¶</a></h2>
|
||||
<p>在交互模式下使用 Python 解释器时,命令的输出是通过内置的 <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> 函数显示的。 在 Python 2.1 中,可以将变量 <a class="reference internal" href="../library/sys.html#sys.displayhook" title="sys.displayhook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.displayhook()</span></code></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> 函数被调用。 例如,你可以将其设置为一个特殊的美化打印函数:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Create a recursive data structure</span>
|
||||
<span class="gp">... </span><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
|
||||
<span class="gp">>>> </span><span class="n">L</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">L</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">L</span> <span class="c1"># Show Python's default output</span>
|
||||
<span class="go">[1, 2, 3, [...]]</span>
|
||||
<span class="gp">>>> </span><span class="c1"># Use pprint.pprint() as the display function</span>
|
||||
<span class="gp">... </span><span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">pprint</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">displayhook</span> <span class="o">=</span> <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span>
|
||||
<span class="gp">>>> </span><span class="n">L</span>
|
||||
<span class="go">[1, 2, 3, <Recursion on list with id=135143996>]</span>
|
||||
<span class="gp">>>></span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-48"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0217"><strong>PEP 217</strong></a> - 用于交互模式的显示钩子</dt><dd><p>由 Moshe Zadka 撰写并实现</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-208-new-coercion-model">
|
||||
<h2>PEP 208: 新的强制转换模型<a class="headerlink" href="#pep-208-new-coercion-model" title="永久链接至标题">¶</a></h2>
|
||||
<p>在 C 级别上的数值类型转换方法进行了重大修改。 这只会影响编写 Python C 扩展的作者,使他们在编写支持数值运算的扩展类型时有更多的灵活性。</p>
|
||||
<p>扩展类型现在可以在其 <code class="docutils literal notranslate"><span class="pre">PyTypeObject</span></code> 结构中设置类型标志 <code class="docutils literal notranslate"><span class="pre">Py_TPFLAGS_CHECKTYPES</span></code>,以表明它们支持新的强制模型。 在此类扩展类型中,数字槽函数不再假定它们将得到两个相同类型的参数;相反,它们可能会得到两个不同类型的参数,然后可以执行自己的内部强制。如果槽函数传递给它一个无法处理的类型,它可以通过返回一个指向 <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</span></code> 单一值的引用来表示失败。 然后将尝试其他类型的数值函数,也许它们可以处理该操作;如果其他类型也返回 <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</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>。 用 Python 写的数值方法也可以返回 <code class="docutils literal notranslate"><span class="pre">Py_NotImplemented</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>,也许会尝试另一个对象的数值方法)。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-49"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0208"><strong>PEP 208</strong></a> - 改写强制转换模型</dt><dd><p>由 Neil Schemenauer 编写和实现,基于 Marc-André Lemburg 的早期工作。阅读这部分内容可以了解数值运算在 C 级别上现在如何处理的细节。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-241-metadata-in-python-packages">
|
||||
<h2>PEP 241: Python 包中的元数据<a class="headerlink" href="#pep-241-metadata-in-python-packages" title="永久链接至标题">¶</a></h2>
|
||||
<p>A common complaint from Python users is that there's no single catalog of all
|
||||
the Python modules in existence. T. Middleton's Vaults of Parnassus at
|
||||
<a class="reference external" href="http://www.vex.net/parnassus/">http://www.vex.net/parnassus/</a> are the largest catalog of Python modules, but
|
||||
registering software at the Vaults is optional, and many people don't bother.</p>
|
||||
<p>作为解决这个问题的第一步,使用 Distutils <strong class="command">sdist</strong> 命令打包的 Python 软件将包含一个名为 <code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> 的文件,其中包含有关包的信息,如名称、版本和作者(在目录编制术语中称为元数据)。<code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> 文件可以包含的字段的完整列表见 <span class="target" id="index-50"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0241"><strong>PEP 241</strong></a>。随着人们开始使用 Python 2.1 打包他们的软件,越来越多的包将包含元数据,从而使得构建自动化目录系统并进行实验成为可能。通过积累经验,也许有可能设计一个真正好的目录系统,然后在 Python 2.2 中支持它。例如,Distutils 的 <strong class="command">sdist</strong> 和 <strong class="command">bdist_*</strong> 命令可以支持一个 <code class="docutils literal notranslate"><span class="pre">upload</span></code> 选项,自动将你的包上传到目录服务器。</p>
|
||||
<p>即使你不使用 Python 2.1,你也可以开始创建包含 <code class="file docutils literal notranslate"><span class="pre">PKG-INFO</span></code> 的包,因为 Distutils 的新版本将为早期 Python 版本的用户发布。Distutils 1.0.2 版本包含了 <span class="target" id="index-51"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0241"><strong>PEP 241</strong></a> 所描述的更改,以及各种错误修复和增强功能。可以从 Distutils SIG 上获取该版本 <a class="reference external" href="https://www.python.org/community/sigs/current/distutils-sig/">https://www.python.org/community/sigs/current/distutils-sig/</a> 。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-52"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0241"><strong>PEP 241</strong></a> - 针对 Python 软件包的元数据</dt><dd><p>由 A.M. Kuchling 撰写并实现。</p>
|
||||
</dd>
|
||||
<dt><span class="target" id="index-53"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0243"><strong>PEP 243</strong></a> - 模块仓库上传机制</dt><dd><p>由 Sean Reifschneider 撰写,这个 PEP 草案描述了用于将 Python 软件包上传到一个中心服务器的建议机制。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="new-and-improved-modules">
|
||||
<h2>新增和改进的模块<a class="headerlink" href="#new-and-improved-modules" title="永久链接至标题">¶</a></h2>
|
||||
<ul>
|
||||
<li><p>Ka-Ping Yee contributed two new modules: <code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect.py</span></code>, a module for
|
||||
getting information about live Python code, and <code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc.py</span></code>, a module for
|
||||
interactively converting docstrings to HTML or text. As a bonus,
|
||||
<code class="file docutils literal notranslate"><span class="pre">Tools/scripts/pydoc</span></code>, which is now automatically installed, uses
|
||||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc.py</span></code> to display documentation given a Python module, package, or
|
||||
class name. For example, <code class="docutils literal notranslate"><span class="pre">pydoc</span> <span class="pre">xml.dom</span></code> displays the following:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Python</span> <span class="n">Library</span> <span class="n">Documentation</span><span class="p">:</span> <span class="n">package</span> <span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="ow">in</span> <span class="n">xml</span>
|
||||
|
||||
<span class="n">NAME</span>
|
||||
<span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="o">-</span> <span class="n">W3C</span> <span class="n">Document</span> <span class="n">Object</span> <span class="n">Model</span> <span class="n">implementation</span> <span class="k">for</span> <span class="n">Python</span><span class="o">.</span>
|
||||
|
||||
<span class="n">FILE</span>
|
||||
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">local</span><span class="o">/</span><span class="n">lib</span><span class="o">/</span><span class="n">python2</span><span class="mf">.1</span><span class="o">/</span><span class="n">xml</span><span class="o">/</span><span class="n">dom</span><span class="o">/</span><span class="fm">__init__</span><span class="o">.</span><span class="n">pyc</span>
|
||||
|
||||
<span class="n">DESCRIPTION</span>
|
||||
<span class="n">The</span> <span class="n">Python</span> <span class="n">mapping</span> <span class="n">of</span> <span class="n">the</span> <span class="n">Document</span> <span class="n">Object</span> <span class="n">Model</span> <span class="ow">is</span> <span class="n">documented</span> <span class="ow">in</span> <span class="n">the</span>
|
||||
<span class="n">Python</span> <span class="n">Library</span> <span class="n">Reference</span> <span class="ow">in</span> <span class="n">the</span> <span class="n">section</span> <span class="n">on</span> <span class="n">the</span> <span class="n">xml</span><span class="o">.</span><span class="n">dom</span> <span class="n">package</span><span class="o">.</span>
|
||||
|
||||
<span class="n">This</span> <span class="n">package</span> <span class="n">contains</span> <span class="n">the</span> <span class="n">following</span> <span class="n">modules</span><span class="p">:</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p><code class="file docutils literal notranslate"><span class="pre">pydoc</span></code> 还包括一个基于 Tk 的交互式帮助浏览器。<code class="file docutils literal notranslate"><span class="pre">pydoc</span></code> 很快会让人上瘾;试试看!</p>
|
||||
</li>
|
||||
<li><p>Two different modules for unit testing were added to the standard library.
|
||||
The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module, contributed by Tim Peters, provides a testing
|
||||
framework based on running embedded examples in docstrings and comparing the
|
||||
results against the expected output. PyUnit, contributed by Steve Purcell, is a
|
||||
unit testing framework inspired by JUnit, which was in turn an adaptation of
|
||||
Kent Beck's Smalltalk testing framework. See <a class="reference external" href="http://pyunit.sourceforge.net/">http://pyunit.sourceforge.net/</a> for
|
||||
more information about PyUnit.</p></li>
|
||||
<li><p>The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module contains a class, <code class="xref py py-class docutils literal notranslate"><span class="pre">SequenceMatcher</span></code>, which
|
||||
compares two sequences and computes the changes required to transform one
|
||||
sequence into the other. For example, this module can be used to write a tool
|
||||
similar to the Unix <strong class="program">diff</strong> program, and in fact the sample program
|
||||
<code class="file docutils literal notranslate"><span class="pre">Tools/scripts/ndiff.py</span></code> demonstrates how to write such a script.</p></li>
|
||||
<li><p><a class="reference internal" href="../library/curses.panel.html#module-curses.panel" title="curses.panel: A panel stack extension that adds depth to curses windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.panel</span></code></a>,是 ncurses 和 SYSV curses 一部分的 panel 库的包装器,由 Thomas Gellekum 贡献。panel 库为窗口提供了深度特性。窗口可以在深度顺序中向上或向下移动,panel 库会计算出面板的重叠位置和哪些部分是可见的。</p></li>
|
||||
<li><p>The PyXML package has gone through a few releases since Python 2.0, and Python
|
||||
2.1 includes an updated version of the <a class="reference internal" href="../library/xml.html#module-xml" title="xml: Package containing XML processing modules"><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml</span></code></a> package. Some of the
|
||||
noteworthy changes include support for Expat 1.2 and later versions, the ability
|
||||
for Expat parsers to handle files in any encoding supported by Python, and
|
||||
various bugfixes for SAX, DOM, and the <code class="xref py py-mod docutils literal notranslate"><span class="pre">minidom</span></code> module.</p></li>
|
||||
<li><p>Ka-Ping Yee 还贡献了另一个用于处理未捕获异常的钩子。<a class="reference internal" href="../library/sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a> 可以设置为一个可调用对象。当异常未被任何 <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a>...<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/sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a>,它可以执行任何需要的操作。在第九届 Python 会议上,他演示了这个钩子的一个应用:打印扩展的回溯信息,不仅列出堆栈帧,还列出每个帧的函数参数和局部变量。</p></li>
|
||||
<li><p>Various functions in the <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> module, such as <code class="xref py py-func docutils literal notranslate"><span class="pre">asctime()</span></code> and
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code>, require a floating point argument containing the time in
|
||||
seconds since the epoch. The most common use of these functions is to work with
|
||||
the current time, so the floating point argument has been made optional; when a
|
||||
value isn't provided, the current time will be used. For example, log file
|
||||
entries usually need a string containing the current time; in Python 2.1,
|
||||
<code class="docutils literal notranslate"><span class="pre">time.asctime()</span></code> can be used, instead of the lengthier
|
||||
<code class="docutils literal notranslate"><span class="pre">time.asctime(time.localtime(time.time()))</span></code> that was previously required.</p>
|
||||
<p>此更改由 Thomas Wouters 提出并实现。</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> 模块现在默认以被动模式检索文件,因为被动模式在防火墙后面更可能正常工作。这一请求来自 Debian 错误跟踪系统,因为其他 Debian 包使用 <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> 来检索文件,但在防火墙后面无法正常工作。由于 Netscape 默认使用被动模式且几乎没有人抱怨,因此认为这不太可能会对任何人造成问题。但如果被动模式不适合你的应用程序或网络设置,可以调用 FTP 对象的 <code class="docutils literal notranslate"><span class="pre">set_pasv(0)</span></code> 来禁用被动模式。</p></li>
|
||||
<li><p>对原始套接字访问的支持已添加到 <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> 模块中,由 Grant Edwards 贡献。</p></li>
|
||||
<li><p><a class="reference internal" href="../library/profile.html#module-pstats" title="pstats: Statistics object for use with the profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pstats</span></code></a> 模块现在包含一个简单的交互式统计浏览器,用于显示 Python 程序的时间分析结果,当该模块作为脚本运行时调用。此功能由 Eric S. Raymond 贡献。</p></li>
|
||||
<li><p>新增了一个依赖于实现的函数 <code class="docutils literal notranslate"><span class="pre">sys._getframe([depth])</span></code>,用于从当前调用堆栈中返回给定的帧对象。<code class="xref py py-func docutils literal notranslate"><span class="pre">sys._getframe`返回调用堆栈顶部的帧对象;如果提供了可选的整数参数</span> <span class="pre">depth,则该函数返回堆栈顶部以下</span> <span class="pre">depth</span> <span class="pre">层的帧。例如,``sys._getframe(1)`()</span></code> 返回调用者的帧对象。</p>
|
||||
<p>这个函数仅存在于 CPython 中,不存在于 Jython 或 .NET 实现中。请将其用于调试,并避免将其放入生产代码中。</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="other-changes-and-fixes">
|
||||
<h2>其他的改变和修正<a class="headerlink" href="#other-changes-and-fixes" title="永久链接至标题">¶</a></h2>
|
||||
<p>由于较短的发布周期,Python 2.1 中的较小更改相对较少。通过搜索 CVS 更改日志,发现应用了 117 个补丁并修复了 136 个错误;这两个数字都可能是低估的。一些较为显著的更改包括:</p>
|
||||
<ul>
|
||||
<li><p>A specialized object allocator is now optionally available, that should be
|
||||
faster than the system <code class="xref py py-func docutils literal notranslate"><span class="pre">malloc()</span></code> and have less memory overhead. The
|
||||
allocator uses C's <code class="xref py py-func docutils literal notranslate"><span class="pre">malloc()</span></code> function to get large pools of memory, and
|
||||
then fulfills smaller memory requests from these pools. It can be enabled by
|
||||
providing the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-pymalloc</span></code> option to the <strong class="program">configure</strong>
|
||||
script; see <code class="file docutils literal notranslate"><span class="pre">Objects/obmalloc.c</span></code> for the implementation details.</p>
|
||||
<p>Authors of C extension modules should test their code with the object allocator
|
||||
enabled, because some incorrect code may break, causing core dumps at runtime.
|
||||
There are a bunch of memory allocation functions in Python's C API that have
|
||||
previously been just aliases for the C library's <code class="xref py py-func docutils literal notranslate"><span class="pre">malloc()</span></code> and
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">free()</span></code>, meaning that if you accidentally called mismatched functions, the
|
||||
error wouldn't be noticeable. When the object allocator is enabled, these
|
||||
functions aren't aliases of <code class="xref py py-func docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">free()</span></code> any more, and
|
||||
calling the wrong function to free memory will get you a core dump. For
|
||||
example, if memory was allocated using <code class="xref py py-func docutils literal notranslate"><span class="pre">PyMem_New()</span></code>, it has to be freed
|
||||
using <code class="xref py py-func docutils literal notranslate"><span class="pre">PyMem_Del()</span></code>, not <code class="xref py py-func docutils literal notranslate"><span class="pre">free()</span></code>. A few modules included with Python
|
||||
fell afoul of this and had to be fixed; doubtless there are more third-party
|
||||
modules that will have the same problem.</p>
|
||||
<p>对象分配器由 Vladimir Marangozov 贡献。</p>
|
||||
</li>
|
||||
<li><p>The speed of line-oriented file I/O has been improved because people often
|
||||
complain about its lack of speed, and because it's often been used as a naïve
|
||||
benchmark. The <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> method of file objects has therefore been
|
||||
rewritten to be much faster. The exact amount of the speedup will vary from
|
||||
platform to platform depending on how slow the C library's <code class="xref py py-func docutils literal notranslate"><span class="pre">getc()</span></code> was, but
|
||||
is around 66%, and potentially much faster on some particular operating systems.
|
||||
Tim Peters did much of the benchmarking and coding for this change, motivated by
|
||||
a discussion in comp.lang.python.</p>
|
||||
<p>A new module and method for file objects was also added, contributed by Jeff
|
||||
Epler. The new method, <code class="xref py py-meth docutils literal notranslate"><span class="pre">xreadlines()</span></code>, is similar to the existing
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code> built-in. <code class="xref py py-func docutils literal notranslate"><span class="pre">xreadlines()</span></code> returns an opaque sequence object
|
||||
that only supports being iterated over, reading a line on every iteration but
|
||||
not reading the entire file into memory as the existing <code class="xref py py-meth docutils literal notranslate"><span class="pre">readlines()</span></code> method
|
||||
does. You'd use it like this:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">xreadlines</span><span class="p">():</span>
|
||||
<span class="c1"># ... do something for each line ...</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>有关行 I/O 更改的更详细讨论,请参阅 2001 年 1 月 1 日至 15 日的 python-dev 摘要 <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2001-January/">https://mail.python.org/pipermail/python-dev/2001-January/</a> 。</p>
|
||||
</li>
|
||||
<li><p>A new method, <code class="xref py py-meth docutils literal notranslate"><span class="pre">popitem()</span></code>, was added to dictionaries to enable
|
||||
destructively iterating through the contents of a dictionary; this can be faster
|
||||
for large dictionaries because there's no need to construct a list containing
|
||||
all the keys or values. <code class="docutils literal notranslate"><span class="pre">D.popitem()</span></code> removes a random <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pair
|
||||
from the dictionary <code class="docutils literal notranslate"><span class="pre">D</span></code> and returns it as a 2-tuple. This was implemented
|
||||
mostly by Tim Peters and Guido van Rossum, after a suggestion and preliminary
|
||||
patch by Moshe Zadka.</p></li>
|
||||
<li><p>模块现在可以通过定义一个 <code class="docutils literal notranslate"><span class="pre">__all__</span></code> 属性来控制使用 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> 时导入的名称。 一个常见的抱怨是,如果模块导入了其他模块,例如 <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> 或 <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a>,使用 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code> 会将它们添加到导入模块的命名空间中。 为了解决这个问题,只需在 <code class="docutils literal notranslate"><span class="pre">__all__</span></code> 模块中列出公共名称即可:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># List public names</span>
|
||||
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Database'</span><span class="p">,</span> <span class="s1">'open'</span><span class="p">]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>此补丁的更严格版本最初由 Ben Wolfson 提出并实现,但在经过一些 python-dev 讨论后,最终版本被修改为较弱的版本并提交。</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">'\012'</span></code>。这是 Python 从 C 语言继承而来的遗留特性,但如今八进制的实际用途非常有限。Ka-Ping Yee 建议使用十六进制转义符代替八进制,并使用 <code class="docutils literal notranslate"><span class="pre">\n</span></code>、<code class="docutils literal notranslate"><span class="pre">\t</span></code>、<code class="docutils literal notranslate"><span class="pre">\r</span></code> 等转义符表示适当的字符,并实现了这种新的格式。</p></li>
|
||||
<li><p>在编译时检测到的语法错误现在可以引发包含错误文件名和行号的异常,这是 Jeremy Hylton 进行的编译器重组的一个令人愉快的副作用。</p></li>
|
||||
<li><p>C extensions which import other modules have been changed to use
|
||||
<code class="xref py py-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code>, which means that they will use any import hooks
|
||||
that have been installed. This is also encouraged for third-party extensions
|
||||
that need to import some other module from C code.</p></li>
|
||||
<li><p>由于 Fredrik Lundh 的努力,Unicode 字符数据库的大小又减少了 340K。</p></li>
|
||||
<li><p>一些新移植版本被贡献:MacOS X(由 Steven Majewski 贡献),Cygwin(由 Jason Tishler 贡献),RISCOS(由 Dietmar Schwertberger 贡献),以及 Unixware 7(由 Billy G. Allie 贡献)。</p></li>
|
||||
</ul>
|
||||
<p>此外还有一份由次要的程序错误修复、次要的内存泄漏、文档字符串编辑和其他调整组成的常规清单,因过于冗长而不值得逐项列出;如果你想了解完整细节请参阅 CVS 日志。</p>
|
||||
</section>
|
||||
<section id="acknowledgements">
|
||||
<h2>致谢<a class="headerlink" href="#acknowledgements" title="永久链接至标题">¶</a></h2>
|
||||
<p>作者感谢以下人员对本文的各种草案提出建议: Graeme Cross, David Goodger, Jay Graves, Michael Hudson, Marc-André Lemburg, Fredrik Lundh, Neil Schemenauer, Thomas Wouters.</p>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h3><a href="../contents.html">目录</a></h3>
|
||||
<ul>
|
||||
<li><a class="reference internal" href="#">Python 2.1 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#introduction">概述</a></li>
|
||||
<li><a class="reference internal" href="#pep-227-nested-scopes">PEP 227: 嵌套的作用域</a></li>
|
||||
<li><a class="reference internal" href="#pep-236-future-directives">PEP 236: __future__ 指令</a></li>
|
||||
<li><a class="reference internal" href="#pep-207-rich-comparisons">PEP 207: 富比较</a></li>
|
||||
<li><a class="reference internal" href="#pep-230-warning-framework">PEP 230: 警告框架</a></li>
|
||||
<li><a class="reference internal" href="#pep-229-new-build-system">PEP 229: 新的构建系统</a></li>
|
||||
<li><a class="reference internal" href="#pep-205-weak-references">PEP 205: 弱引用</a></li>
|
||||
<li><a class="reference internal" href="#pep-232-function-attributes">PEP 232: 函数属性</a></li>
|
||||
<li><a class="reference internal" href="#pep-235-importing-modules-on-case-insensitive-platforms">PEP 235: 在大小写不敏感的平台上导入模块</a></li>
|
||||
<li><a class="reference internal" href="#pep-217-interactive-display-hook">PEP 217: 交互模式显示钩子</a></li>
|
||||
<li><a class="reference internal" href="#pep-208-new-coercion-model">PEP 208: 新的强制转换模型</a></li>
|
||||
<li><a class="reference internal" href="#pep-241-metadata-in-python-packages">PEP 241: Python 包中的元数据</a></li>
|
||||
<li><a class="reference internal" href="#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li><a class="reference internal" href="#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="2.2.html"
|
||||
title="上一章">Python 2.2 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="2.0.html"
|
||||
title="下一章">Python 2.0 有什么新变化</a></p>
|
||||
<div role="note" aria-label="source link">
|
||||
<h3>本页</h3>
|
||||
<ul class="this-page-menu">
|
||||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||||
<li>
|
||||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/2.1.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.0.html" title="Python 2.0 有什么新变化"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="2.2.html" title="Python 2.2 有什么新变化"
|
||||
>上一页</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>
|
||||
1065
web/python-docs/whatsnew/2.2.html
Normal file
1065
web/python-docs/whatsnew/2.2.html
Normal file
File diff suppressed because it is too large
Load Diff
1945
web/python-docs/whatsnew/2.3.html
Normal file
1945
web/python-docs/whatsnew/2.3.html
Normal file
File diff suppressed because it is too large
Load Diff
1634
web/python-docs/whatsnew/2.4.html
Normal file
1634
web/python-docs/whatsnew/2.4.html
Normal file
File diff suppressed because it is too large
Load Diff
2256
web/python-docs/whatsnew/2.5.html
Normal file
2256
web/python-docs/whatsnew/2.5.html
Normal file
File diff suppressed because it is too large
Load Diff
3099
web/python-docs/whatsnew/2.6.html
Normal file
3099
web/python-docs/whatsnew/2.6.html
Normal file
File diff suppressed because it is too large
Load Diff
2582
web/python-docs/whatsnew/2.7.html
Normal file
2582
web/python-docs/whatsnew/2.7.html
Normal file
File diff suppressed because it is too large
Load Diff
957
web/python-docs/whatsnew/3.0.html
Normal file
957
web/python-docs/whatsnew/3.0.html
Normal file
@@ -0,0 +1,957 @@
|
||||
|
||||
<!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>
|
||||
620
web/python-docs/whatsnew/3.1.html
Normal file
620
web/python-docs/whatsnew/3.1.html
Normal file
@@ -0,0 +1,620 @@
|
||||
|
||||
<!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.1 有什么新变化 — 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 3.0 有什么新变化" href="3.0.html" />
|
||||
<link rel="prev" title="Python 3.2 有什么新变化" href="3.2.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.1.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.1 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#pep-372-ordered-dictionaries">PEP 372: 有序字典</a></li>
|
||||
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
|
||||
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
|
||||
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
|
||||
<li><a class="reference internal" href="#idle">IDLE</a></li>
|
||||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li><a class="reference internal" href="#porting-to-python-3-1">移植到 Python 3.1</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="3.2.html"
|
||||
title="上一章">Python 3.2 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="3.0.html"
|
||||
title="下一章">Python 3.0 有什么新变化</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.1.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.1.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="3.0.html" title="Python 3.0 有什么新变化"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="3.2.html" title="Python 3.2 有什么新变化"
|
||||
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-1">
|
||||
<h1>Python 3.1 有什么新变化<a class="headerlink" href="#what-s-new-in-python-3-1" title="永久链接至标题">¶</a></h1>
|
||||
<dl class="field-list simple">
|
||||
<dt class="field-odd">作者</dt>
|
||||
<dd class="field-odd"><p>Raymond Hettinger(译者:wh2099 at outlook dot com)</p>
|
||||
</dd>
|
||||
</dl>
|
||||
<p>This article explains the new features in Python 3.1, compared to 3.0.</p>
|
||||
<section id="pep-372-ordered-dictionaries">
|
||||
<h2>PEP 372: 有序字典<a class="headerlink" href="#pep-372-ordered-dictionaries" title="永久链接至标题">¶</a></h2>
|
||||
<p>常规的 Python 字典会以任意顺序迭代键/值对。 多年以来,有好几位作者编写了可以记住键的初始插入顺序的替代实现。 基于这些实现的经验,现在引入了新的 <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a> 类。</p>
|
||||
<p>OrderedDict API 与常规字典基本相同,但将根据每个键首次插入的时间以有保证的顺序来迭代键和值。 如果一个新条目覆盖了现有的条目,则原始插入位置会保持不变。 删除条目并重新插入则会将其移至末尾。</p>
|
||||
<p>The standard library now supports use of ordered dictionaries in several
|
||||
modules. The <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a> module uses them by default. This lets
|
||||
configuration files be read, modified, and then written back in their original
|
||||
order. The <em>_asdict()</em> method for <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a> now
|
||||
returns an ordered dictionary with the values appearing in the same order as
|
||||
the underlying tuple indices. The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module is being built-out with
|
||||
an <em>object_pairs_hook</em> to allow OrderedDicts to be built by the decoder.
|
||||
Support was also added for third-party tools like <a class="reference external" href="http://pyyaml.org/">PyYAML</a>.</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0372"><strong>PEP 372</strong></a> - 有序字典</dt><dd><p>PEP 由 Armin Ronacher 和 Raymond Hettinger 撰写,由 Raymond Hettinger 实现。</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="pep-378-format-specifier-for-thousands-separator">
|
||||
<h2>PEP 378: 千位分隔符的格式说明符<a class="headerlink" href="#pep-378-format-specifier-for-thousands-separator" title="永久链接至标题">¶</a></h2>
|
||||
<p>内置的 <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> 函数和 <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> 方法使用的迷你语言现在包括一种以千位分隔符格式化数字的简单、不可感知语言区域的方式。 这提供了更为人性化的程序输出,提升了外观专业程度与可读性:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="mi">1234567</span><span class="p">,</span> <span class="s1">',d'</span><span class="p">)</span>
|
||||
<span class="go">'1,234,567'</span>
|
||||
<span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="mf">1234567.89</span><span class="p">,</span> <span class="s1">',.2f'</span><span class="p">)</span>
|
||||
<span class="go">'1,234,567.89'</span>
|
||||
<span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="mf">12345.6</span> <span class="o">+</span> <span class="mf">8901234.12</span><span class="n">j</span><span class="p">,</span> <span class="s1">',f'</span><span class="p">)</span>
|
||||
<span class="go">'12,345.600000+8,901,234.120000j'</span>
|
||||
<span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s1">'1234567.89'</span><span class="p">),</span> <span class="s1">',f'</span><span class="p">)</span>
|
||||
<span class="go">'1,234,567.89'</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>支持的类型有 <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#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>, <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a> 和 <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a>。</p>
|
||||
<p>目前还在讨论要如何指定替代分隔符如点号、空格、撇号或下划线等。 可感知语言区域的应用程序应当使用现有的支持千位分隔符的 <em>n</em> 格式说明符。</p>
|
||||
<div class="admonition seealso">
|
||||
<p class="admonition-title">参见</p>
|
||||
<dl class="simple">
|
||||
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a> - 千位分隔符的格式说明符</dt><dd><p>PEP 由 Raymond Hettinger 撰写,并由 Eric Smith 和 Mark Dickinson 实现</p>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</section>
|
||||
<section id="other-language-changes">
|
||||
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题">¶</a></h2>
|
||||
<p>对Python 语言核心进行的小改动:</p>
|
||||
<ul>
|
||||
<li><p>包括 <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件的目录和 zip 归档文件现在可通过将其名称传给解释器来直接执行。 这样的目录/zip 文件会自动作为 sys.path 的第一个条目插入。 (由 Andy Chu 提议并编写初始补丁;由 Phillip J. Eby 和 Nick Coghlan 改进补丁;<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1739468">bpo-1739468</a>。)</p></li>
|
||||
<li><p><a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> 类型增加了一个 <code class="docutils literal notranslate"><span class="pre">bit_length</span></code> 方法用来返回以二进制代表其参数值所需的比特位数量:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">n</span> <span class="o">=</span> <span class="mi">37</span>
|
||||
<span class="gp">>>> </span><span class="nb">bin</span><span class="p">(</span><span class="mi">37</span><span class="p">)</span>
|
||||
<span class="go">'0b100101'</span>
|
||||
<span class="gp">>>> </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
|
||||
<span class="go">6</span>
|
||||
<span class="gp">>>> </span><span class="n">n</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">123</span><span class="o">-</span><span class="mi">1</span>
|
||||
<span class="gp">>>> </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
|
||||
<span class="go">123</span>
|
||||
<span class="gp">>>> </span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
|
||||
<span class="go">124</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Fredrik Johansson, Victor Stinner, Raymond Hettinger 和 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=3439">bpo-3439</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> 字符串中的字段现在可被自动编号:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'Sir </span><span class="si">{}</span><span class="s1"> of </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'Gallahad'</span><span class="p">,</span> <span class="s1">'Camelot'</span><span class="p">)</span>
|
||||
<span class="go">'Sir Gallahad of Camelot'</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>之前,字符串应当具有所需的编号字段例如: <code class="docutils literal notranslate"><span class="pre">'Sir</span> <span class="pre">{0}</span> <span class="pre">of</span> <span class="pre">{1}'</span></code>。</p>
|
||||
<p>(由 Eric Smith在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5237">bpo-5237</a> 中贡献)</p>
|
||||
</li>
|
||||
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">string.maketrans()</span></code> 函数已被弃用并由新的静态方法 <a class="reference internal" href="../library/stdtypes.html#bytes.maketrans" title="bytes.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.maketrans()</span></code></a> 和 <a class="reference internal" href="../library/stdtypes.html#bytearray.maketrans" title="bytearray.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytearray.maketrans()</span></code></a> 取代。 这项修改解决了对于 <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> 模块支持类型的困惑。 现在,<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> 和 <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> 分别拥有它们自己的附带适当类型的中间转写表的 <strong>maketrans</strong> 和 <strong>translate</strong> 方法。</p>
|
||||
<p>(由Georg Brandl在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5675">bpo-5675</a> 中贡献)</p>
|
||||
</li>
|
||||
<li><p>The syntax of 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> 语句的语法现在允许单个语句中存在多个上下文管理器:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'mylog.txt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">infile</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'a.out'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
|
||||
<span class="gp">... </span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">infile</span><span class="p">:</span>
|
||||
<span class="gp">... </span> <span class="k">if</span> <span class="s1">'<critical>'</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
|
||||
<span class="gp">... </span> <span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>有了这个新语法,<code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code> 函数已不再必要因而现在已被弃用。</p>
|
||||
<p>(由 Georg Brandl 和 Mattias Brändström 贡献; <a class="reference external" href="https://codereview.appspot.com/53094">appspot issue 53094</a>。)</p>
|
||||
</li>
|
||||
<li><p>现在 <code class="docutils literal notranslate"><span class="pre">round(x,</span> <span class="pre">n)</span></code> 当 <em>x</em> 为整数时将返回整数。 之前是返回浮点数:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="mi">1123</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
|
||||
<span class="go">1100</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Mark Dickinson在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4707">bpo-4707</a> 贡献)</p>
|
||||
</li>
|
||||
<li><p>Python 现在使用 David Gay 的算法来查找不会改变实际值的最短浮点表示形式。 这应当有助于缓解某些对于二进制浮点数的困惑。</p>
|
||||
<p>这项改进的优点对于 <code class="docutils literal notranslate"><span class="pre">1.1</span></code> 这样无法用二进制浮点精确表示的数来说是很明显的。 由于没有完全等价的表示,<code class="docutils literal notranslate"><span class="pre">float('1.1')</span></code> 这样的表达式会被求解为最接近的可表示值即十六进制的 <code class="docutils literal notranslate"><span class="pre">0x1.199999999999ap+0</span></code> 或十进制的 <code class="docutils literal notranslate"><span class="pre">1.100000000000000088817841970012523233890533447265625</span></code>。 这个最接近的值过去和现在仍然会在后续的浮点运算中被使用。</p>
|
||||
<p>新的改变针对的是如何显示数字。 在之前,Python 使用了一种简单的方式。 <code class="docutils literal notranslate"><span class="pre">repr(1.1)</span></code> 的值会被计算为 <code class="docutils literal notranslate"><span class="pre">format(1.1,</span> <span class="pre">'.17g')</span></code> 并将被求解为 <code class="docutils literal notranslate"><span class="pre">'1.1000000000000001'</span></code>。 使用 17 个数位的优点是它将凭借 IEEE-754 标准来确保 <code class="docutils literal notranslate"><span class="pre">eval(repr(1.1))</span></code> 将恰好被舍入到其原始值。 缺点则是会让许多人感觉这样的输出令人困惑(将二进制浮点表示形式的内在局限性误认为是 Python 本身的问题)。</p>
|
||||
<p>用于 <code class="docutils literal notranslate"><span class="pre">repr(1.1)</span></code> 的新算法更为智能并将返回 <code class="docutils literal notranslate"><span class="pre">'1.1'</span></code>。 在实际上,它会搜索所有等价的字符串表示形式(使用相同的下层浮点值进行排序)并返回其中最短的表示形式。</p>
|
||||
<p>新算法倾向于尽可能放出更为清晰的表示形式,但它并不改变下层的值。 因此仍然会是 <code class="docutils literal notranslate"><span class="pre">1.1</span> <span class="pre">+</span> <span class="pre">2.2</span> <span class="pre">!=</span> <span class="pre">3.3</span></code> 虽然从表示形式上看情况不是这样。</p>
|
||||
<p>这个新算法依赖于下层浮点实现的某些特性。 如果未找到所需的特性,则将继续使用旧算法。 此外,文本 pickle 协议也会通过使用旧算法来保证跨平台的可移植性。</p>
|
||||
<p>(由 Eric Smith 和 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1580">bpo-1580</a> 贡献)</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="new-improved-and-deprecated-modules">
|
||||
<h2>新增,改进和弃用的模块<a class="headerlink" href="#new-improved-and-deprecated-modules" title="永久链接至标题">¶</a></h2>
|
||||
<ul>
|
||||
<li><p>增加了一个 <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.Counter</span></code></a> 类以支持方便地统计一个序列或可迭代对象中的唯一条目数量。:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Counter</span><span class="p">([</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">,</span> <span class="s1">'red'</span><span class="p">,</span> <span class="s1">'green'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">])</span>
|
||||
<span class="go">Counter({'blue': 3, 'red': 2, 'green': 1})</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1696199">bpo-1696199</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>半圆了一个新模块 <a class="reference internal" href="../library/tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code></a> 用于访问带主题的 Tk 部件集。 ttk 的基本设计思路,就是尽可能地把实现部件行为的代码与实现其外观的代码分离开来。</p>
|
||||
<p>(由 Guilherme Polo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=2983">bpo-2983</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">gzip.GzipFile</span></code></a> 和 <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> 类现在已支持上下文管理协议:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Automatically close file after writing</span>
|
||||
<span class="gp">>>> </span><span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">GzipFile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">"wb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
||||
<span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">"xxx"</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Antoine Pitrou 贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> 模块现在支持基于一个二进制 <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> 来创建 decimal 对象。 转换是准确的但有时也会令人吃惊:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
|
||||
<span class="go">Decimal('1.100000000000000088817841970012523233890533447265625')</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>这个长长的 decimal 结果值显示了 <em>1.1</em> 所保存的实际二进制分数。 这个分数有许多位因为 <em>1.1</em> 无法用二进制来精确表示。</p>
|
||||
<p>(由Raymond Hettinger 和 Mark Dickinson贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> 模块增加了两个新函数。 <a class="reference internal" href="../library/itertools.html#itertools.combinations_with_replacement" title="itertools.combinations_with_replacement"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.combinations_with_replacement()</span></code></a> 函数是生成组合数学结果包括排列与笛卡尔积的四个函数之一。 <a class="reference internal" href="../library/itertools.html#itertools.compress" title="itertools.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.compress()</span></code></a> 函数模仿了 APL 中的同名函数。 此外,现有的 <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.count()</span></code></a> 函数现在有一个可选的 <em>step</em> 参数并可接受任意类型的计数序列包括 <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">fractions.Fraction</span></code></a> 和 <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a>:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">[</span><span class="n">p</span><span class="o">+</span><span class="n">q</span> <span class="k">for</span> <span class="n">p</span><span class="p">,</span><span class="n">q</span> <span class="ow">in</span> <span class="n">combinations_with_replacement</span><span class="p">(</span><span class="s1">'LOVE'</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>
|
||||
<span class="go">['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']</span>
|
||||
|
||||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">compress</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">selectors</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]))</span>
|
||||
<span class="go">[2, 3, 5, 7]</span>
|
||||
|
||||
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">count</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">Fraction</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="n">step</span><span class="o">=</span><span class="n">Fraction</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
|
||||
<span class="gp">>>> </span><span class="p">[</span><span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">)]</span>
|
||||
<span class="go">[Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Raymond Hettinger 贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a> 现在支持关键字参数 <em>rename</em>,它允许将无效的字段名自动转换为 _0, _1 等形式的位置名称。 这在字段名是由外部源如 CSV 标头、SQL 字段列表或用户输入创建的时候会很有用处:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">query</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
|
||||
<span class="go">SELECT region, dept, count(*) FROM main GROUPBY region, dept</span>
|
||||
|
||||
<span class="gp">>>> </span><span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">query_fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">desc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">desc</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">description</span><span class="p">]</span>
|
||||
<span class="gp">>>> </span><span class="n">UserQuery</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">'UserQuery'</span><span class="p">,</span> <span class="n">query_fields</span><span class="p">,</span> <span class="n">rename</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||||
<span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">([</span><span class="n">UserQuery</span><span class="p">(</span><span class="o">*</span><span class="n">row</span><span class="p">)</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">])</span>
|
||||
<span class="go">[UserQuery(region='South', dept='Shipping', _2=185),</span>
|
||||
<span class="go"> UserQuery(region='North', dept='Accounting', _2=37),</span>
|
||||
<span class="go"> UserQuery(region='West', dept='Sales', _2=419)]</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1818">bpo-1818</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.sub()</span></code></a>, <a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.subn()</span></code></a> 和 <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.split()</span></code></a> 函数现在可接受一个 flags 形参。</p>
|
||||
<p>(由 Gregory Smith 贡献)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> 模块现在为不使用 logging 但是调用了使用它的库代码的应用程序实现了一个简单的 <a class="reference internal" href="../library/logging.handlers.html#logging.NullHandler" title="logging.NullHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">logging.NullHandler</span></code></a> 类。 设置一个空处理器将会屏蔽诸如 "找不到日志记录器 foo 的处理器" 这样的虚假警告:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">NullHandler</span><span class="p">()</span>
|
||||
<span class="gp">>>> </span><span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Vinay Sajip 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4384">bpo-4384</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>支持 <code class="docutils literal notranslate"><span class="pre">-m</span></code> 命令行开关的 <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> 模块现在也支持当提供包名称时通过查找并执行 <code class="docutils literal notranslate"><span class="pre">__main__</span></code> 子模块来执行包。</p>
|
||||
<p>(由 Andi Vajda 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4195">bpo-4195</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> 模块现在可以访问并显示通过 <a class="reference internal" href="../library/zipimport.html#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a> (或其他符合规范的 <span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> 加载器) 加载的源代码。</p>
|
||||
<p>(由 Alexander Belopolsky 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4201">bpo-4201</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/functools.html#functools.partial" title="functools.partial"><code class="xref py py-class docutils literal notranslate"><span class="pre">functools.partial</span></code></a> 对象现在可以被封存。</p></li>
|
||||
</ul>
|
||||
<blockquote>
|
||||
<div><p>(由 Antoine Pitrou 和 Jesse Noller 提议,由 Jack Diederich 实现; <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5228">bpo-5228</a>。)</p>
|
||||
</div></blockquote>
|
||||
<ul>
|
||||
<li><p>为符号增加 <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 帮助主题以使得在交互环境下 <code class="docutils literal notranslate"><span class="pre">help('@')</span></code> 能符合预期的效果。</p>
|
||||
<p>(由 David Laban 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4739">bpo-4739</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> 模块现在支持跳过单个测试或测试类。 并且它还支持将一个测试标记为已预期会失败,即已经知道不可用,但不应在 TestResult 上被计为一次失败:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TestGizmo</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
|
||||
|
||||
<span class="nd">@unittest</span><span class="o">.</span><span class="n">skipUnless</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"win"</span><span class="p">),</span> <span class="s2">"requires Windows"</span><span class="p">)</span>
|
||||
<span class="k">def</span> <span class="nf">test_gizmo_on_windows</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
|
||||
<span class="nd">@unittest</span><span class="o">.</span><span class="n">expectedFailure</span>
|
||||
<span class="k">def</span> <span class="nf">test_gimzo_without_required_library</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>此外,还创建了一些针对异常的测试以便与使用 <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> 语句的上下文管理器一起工作:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_division_by_zero</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
|
||||
<span class="n">x</span> <span class="o">/</span> <span class="mi">0</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>此外,还增加了一些新的断言方法包括 <code class="xref py py-func docutils literal notranslate"><span class="pre">assertSetEqual()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertDictEqual()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertDictContainsSubset()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertListEqual()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertTupleEqual()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertSequenceEqual()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertRaisesRegexp()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">assertIsNone()</span></code> 以及 <code class="xref py py-func docutils literal notranslate"><span class="pre">assertIsNotNone()</span></code>。</p>
|
||||
<p>(由Benjamin Peterson 和 Antoine Pitrou 贡献。)</p>
|
||||
</li>
|
||||
<li><p><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> 模块新增了三个常量来代表 <code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code> 方法 <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_SET</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_CUR</span></code> 和 <code class="xref py py-data docutils literal notranslate"><span class="pre">SEEK_END</span></code>。</p></li>
|
||||
<li><p><a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> 元组现在是一个具名元组:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span>
|
||||
<span class="go">sys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Ross Light 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4285">bpo-4285</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a> 和 <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> 模块现在支持 IPv6。</p>
|
||||
<p>(由 Derek Morr 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1655">bpo-1655</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1664">bpo-1664</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>当使用协议 2 或更低的版本时,<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> 模块已被调适以获得与 Python 2.x 更好的互操作性。 标准库的重组改变了对于许多对象的正式引用。 例如,Python 2 中的 <code class="docutils literal notranslate"><span class="pre">__builtin__.set</span></code> 在 Python 3 中称为 <code class="docutils literal notranslate"><span class="pre">builtins.set</span></code>。 这一改变使得在不同版本的 Python 之间共享数据的努力陷入混乱。 但是现在当选择协议 2 或更低的版本时,封存器将自动使用旧的 Python 2 名称进行加载和转储。 这样的重映射将默认启用但可以通过 <em>fix_imports</em> 选项来禁用:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">}</span>
|
||||
<span class="gp">>>> </span><span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
||||
<span class="go">b'c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'</span>
|
||||
<span class="gp">>>> </span><span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">fix_imports</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
||||
<span class="go">b'cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>这项改变的一个不幸但无可避免的副作用是由 Python 3.1 所产生的协议 2 版本的 pickle 对象对于 Python 3.0 将不可读。 当在 Python 3.x 实现之间迁移数据时,应当使用最新的协议 3 版本的 pickle 协议,因为它不会试图与 Python 2.x 保持兼容。</p>
|
||||
<p>(由 Alexandre Vassalotti 和 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=6137">bpo-6137</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>增加了一个新模块 <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>。 它提供了针对 <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> 语句及其对应物 <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> 函数的完整、可移植的纯 Python 引用实现。 它代表了在记录和定义导入期间所发生的行动中实质性的一步。</p>
|
||||
<p>(由 Brett Cannon 贡献。)</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="optimizations">
|
||||
<h2>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题">¶</a></h2>
|
||||
<p>已增加的主要性能改善:</p>
|
||||
<ul>
|
||||
<li><p>新的 I/O 库(如 <span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3116"><strong>PEP 3116</strong></a> 所定义的)主要是用 Python 编写并很快被证明是 Python 3.0 中的一个问题瓶颈。 在 Python 3.1 中,I/O 库已完全用 C 重写并根据具体任务的不同有 2 到 20 倍的速度提升。 纯 Python 版本仍然可通过 <code class="docutils literal notranslate"><span class="pre">_pyio</span></code> 模块访问以用于实验性目的。</p>
|
||||
<p>(由 Amaury Forgeot d'Arc 和 Antoine Pitrou 贡献。)</p>
|
||||
</li>
|
||||
<li><p>添加了一个启发式的工具以使仅包含不可追踪对象的元组和字典不会被垃圾回收器所追踪。 这可以减少收集数据的大小从而减少长时间运行的程序的垃圾收集开销,具体取决于它们对类型类型的使用方式。</p>
|
||||
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4688">bpo-4688</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>通过在受支持的编译器(主要有: gcc, SunPro, icc)上启用名为 <code class="docutils literal notranslate"><span class="pre">--with-computed-gotos</span></code> 的配置选项,字节码求值循环会使用新的分派机制进行编译,根据系统、编译器和基准测试工具的不同,该机制可获得至多 20% 的速度提升。</p>
|
||||
<p>(由 Antoine Pitrou 以及其他一些参与者在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4753">bpo-4753</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>UTF-8, UTF-16 和 LATIN-1 的解码速度现已提升了二至四倍。</p>
|
||||
<p>(由 Antoine Pitrou 和 Amaury Forgeot d'Arc 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4868">bpo-4868</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module now has a C extension to substantially improve
|
||||
its performance. In addition, the API was modified so that json works
|
||||
only with <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>, not with <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>. That change makes the
|
||||
module closely match the <a class="reference external" href="http://json.org/">JSON specification</a>
|
||||
which is defined in terms of Unicode.</p>
|
||||
<p>(由 Bob Ippolito 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4136">bpo-4136</a> 中贡献。并由 Antoine Pitrou 和 Benjamin Peterson 转换为Py3.1)</p>
|
||||
</li>
|
||||
<li><p>解封操作现在将固定已封存对象的属性名称。 这可以节省内存并让封存对象变得更小。</p>
|
||||
<p>(由 Jake McGuire 和 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5084">bpo-5084</a> 中贡献。)</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="idle">
|
||||
<h2>IDLE<a class="headerlink" href="#idle" title="永久链接至标题">¶</a></h2>
|
||||
<ul>
|
||||
<li><p>IDLE 的格式菜单现在提供了一个从源文件中去除尾部空格的选项。</p>
|
||||
<p>(由 Roger D. Serwy 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5150">bpo-5150</a> 中贡献。)</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="build-and-c-api-changes">
|
||||
<h2>构建和 C API 的改变<a class="headerlink" href="#build-and-c-api-changes" title="永久链接至标题">¶</a></h2>
|
||||
<p>针对 Python 构建过程和 C API 的改变包括:</p>
|
||||
<ul>
|
||||
<li><p>Integers are now stored internally either in base 2**15 or in base
|
||||
2**30, the base being determined at build time. Previously, they
|
||||
were always stored in base 2**15. Using base 2**30 gives
|
||||
significant performance improvements on 64-bit machines, but
|
||||
benchmark results on 32-bit machines have been mixed. Therefore,
|
||||
the default is to use base 2**30 on 64-bit machines and base 2**15
|
||||
on 32-bit machines; on Unix, there's a new configure option
|
||||
<code class="docutils literal notranslate"><span class="pre">--enable-big-digits</span></code> that can be used to override this default.</p>
|
||||
<p>除了性能提升之外这项改变对最终用户来说应当是不可见的,只有一个例外:出于测试和调试目的有一个新的提供相关内部格式信息的 <a class="reference internal" href="../library/sys.html#sys.int_info" title="sys.int_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.int_info</span></code></a>,它给出了每个数位对应的比特位数和用于存储每个数位的以字节数表示的 C 类型大小:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
|
||||
<span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">int_info</span>
|
||||
<span class="go">sys.int_info(bits_per_digit=30, sizeof_digit=4)</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
<p>(由 Mark Dickinson在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4258">bpo-4258</a> 贡献)</p>
|
||||
</li>
|
||||
<li><p><a class="reference internal" href="../c-api/long.html#c.PyLong_AsUnsignedLongLong" title="PyLong_AsUnsignedLongLong"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsUnsignedLongLong()</span></code></a> 函数现在将通过引发 <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 而不是 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> 来处理负的 <em>pylong</em>。</p>
|
||||
<p>(由 Mark Dickinson 和 Lisandro Dalcrin 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5175">bpo-5175</a> 中贡献。)</p>
|
||||
</li>
|
||||
<li><p>已弃用 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Int()</span></code> 。请改用 <a class="reference internal" href="../c-api/number.html#c.PyNumber_Long" title="PyNumber_Long"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Long()</span></code></a> 。</p>
|
||||
<p>(由 Mark Dickinson在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4910">bpo-4910</a> 贡献)</p>
|
||||
</li>
|
||||
<li><p>增加了一个新的 <a class="reference internal" href="../c-api/conversion.html#c.PyOS_string_to_double" title="PyOS_string_to_double"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_string_to_double()</span></code></a> 函数来替代已弃用的函数 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_strtod()</span></code> 和 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_atof()</span></code>。</p>
|
||||
<p>(由 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5914">bpo-5914</a> 贡献)</p>
|
||||
</li>
|
||||
<li><p>增加了 <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> 作为 <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> API 的替代。 主要区别在于新类型拥有一个定义良好的接口用来传递类型安全信息并拥有一个较低复杂度的签名用来调用析构器。 旧类型的 API 存在问题且现在已被弃用。</p>
|
||||
<p>(由 Larry Hastings 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5630">bpo-5630</a> 中贡献。)</p>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
<section id="porting-to-python-3-1">
|
||||
<h2>移植到 Python 3.1<a class="headerlink" href="#porting-to-python-3-1" title="永久链接至标题">¶</a></h2>
|
||||
<p>本节列出了先前描述的改变以及可能需要修改你的代码的其他问题修正:</p>
|
||||
<ul>
|
||||
<li><p>新的浮点字符串表示形式可能会破坏现有的 doctest。 例如:</p>
|
||||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">e</span><span class="p">():</span>
|
||||
<span class="w"> </span><span class="sd">'''Compute the base of natural logarithms.</span>
|
||||
|
||||
<span class="sd"> >>> e()</span>
|
||||
<span class="sd"> 2.7182818284590451</span>
|
||||
|
||||
<span class="sd"> '''</span>
|
||||
<span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">math</span><span class="o">.</span><span class="n">factorial</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)))</span>
|
||||
|
||||
<span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
|
||||
|
||||
<span class="o">**********************************************************************</span>
|
||||
<span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>
|
||||
<span class="n">e</span><span class="p">()</span>
|
||||
<span class="n">Expected</span><span class="p">:</span>
|
||||
<span class="mf">2.7182818284590451</span>
|
||||
<span class="n">Got</span><span class="p">:</span>
|
||||
<span class="mf">2.718281828459045</span>
|
||||
<span class="o">**********************************************************************</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</li>
|
||||
<li><p>在 pickle 模块中用于协议 2 或更低版本的自动名称重映射会使得 Python 3.1 的 pickle 在 Python 3.0 中无法读取。 一种解决方案是使用协议 3。 另一种解决方案是将 <em>fix_imports</em> 选项设为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。 请参阅上面的讨论来了解更多细节。</p></li>
|
||||
</ul>
|
||||
</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.1 有什么新变化</a><ul>
|
||||
<li><a class="reference internal" href="#pep-372-ordered-dictionaries">PEP 372: 有序字典</a></li>
|
||||
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
|
||||
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
|
||||
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
|
||||
<li><a class="reference internal" href="#idle">IDLE</a></li>
|
||||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li><a class="reference internal" href="#porting-to-python-3-1">移植到 Python 3.1</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="3.2.html"
|
||||
title="上一章">Python 3.2 有什么新变化</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="3.0.html"
|
||||
title="下一章">Python 3.0 有什么新变化</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.1.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="3.0.html" title="Python 3.0 有什么新变化"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="3.2.html" title="Python 3.2 有什么新变化"
|
||||
>上一页</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>
|
||||
2820
web/python-docs/whatsnew/3.2.html
Normal file
2820
web/python-docs/whatsnew/3.2.html
Normal file
File diff suppressed because it is too large
Load Diff
2582
web/python-docs/whatsnew/3.3.html
Normal file
2582
web/python-docs/whatsnew/3.3.html
Normal file
File diff suppressed because it is too large
Load Diff
1769
web/python-docs/whatsnew/3.4.html
Normal file
1769
web/python-docs/whatsnew/3.4.html
Normal file
File diff suppressed because it is too large
Load Diff
1839
web/python-docs/whatsnew/3.5.html
Normal file
1839
web/python-docs/whatsnew/3.5.html
Normal file
File diff suppressed because one or more lines are too long
1704
web/python-docs/whatsnew/3.6.html
Normal file
1704
web/python-docs/whatsnew/3.6.html
Normal file
File diff suppressed because it is too large
Load Diff
1510
web/python-docs/whatsnew/3.7.html
Normal file
1510
web/python-docs/whatsnew/3.7.html
Normal file
File diff suppressed because it is too large
Load Diff
1639
web/python-docs/whatsnew/3.8.html
Normal file
1639
web/python-docs/whatsnew/3.8.html
Normal file
File diff suppressed because it is too large
Load Diff
21016
web/python-docs/whatsnew/changelog.html
Normal file
21016
web/python-docs/whatsnew/changelog.html
Normal file
File diff suppressed because it is too large
Load Diff
686
web/python-docs/whatsnew/index.html
Normal file
686
web/python-docs/whatsnew/index.html
Normal file
@@ -0,0 +1,686 @@
|
||||
|
||||
<!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的新变化 — 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 3.8 有什么新变化" href="3.8.html" />
|
||||
<link rel="prev" title="Python 文档目录" href="../contents.html" />
|
||||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/index.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>
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="../contents.html"
|
||||
title="上一章">Python 文档目录</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="3.8.html"
|
||||
title="下一章">Python 3.8 有什么新变化</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/index.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/index.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="3.8.html" title="Python 3.8 有什么新变化"
|
||||
accesskey="N">下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../contents.html" title="Python 文档目录"
|
||||
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="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">
|
||||
<span id="whatsnew-index"></span><h1>Python的新变化<a class="headerlink" href="#what-s-new-in-python" title="永久链接至标题">¶</a></h1>
|
||||
<p>这个“Python 有什么新变化?”系列内容会带您浏览 Python 大版本之间重要的变化。在新版发布后,如果您希望掌握最新变化,请务必阅读这些内容。</p>
|
||||
<div class="toctree-wrapper compound">
|
||||
<ul>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.8.html">Python 3.8 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#new-features">新的特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#deprecated">弃用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#api-and-feature-removals">API 与特性的移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#porting-to-python-3-8">移植到 Python 3.8</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-1">Python 3.8.1 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-2">Python 3.8.2 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-3">Python 3.8.3 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-8">Python 3.8.8 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-9">Python 3.8.9 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-10">Python 3.8.10 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#id1">Python 3.8.10 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-python-3-8-12">Python 3.8.12 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-security-feature-in-3-8-14">3.8.14 中的重要安全特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-3-8-17">Notable Changes in 3.8.17</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.8.html#notable-changes-in-3-8-20">Notable changes in 3.8.20</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.7.html">Python 3.7 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#summary-release-highlights">摘要 - 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#new-features">新的特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#c-api-changes">C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#build-changes">构建的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#other-cpython-implementation-changes">其他 CPython 实现的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#deprecated-python-behavior">已弃用的 Python 行为</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#deprecated-python-modules-functions-and-methods">已弃用的 Python 模块、函数和方法</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#deprecated-functions-and-types-of-the-c-api">已弃用的 C API 函数和类型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#platform-support-removals">平台支持的移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#api-and-feature-removals">API 与特性的移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#module-removals">移除的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#windows-only-changes">Windows 专属的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#porting-to-python-3-7">移植到 Python 3.7</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#notable-changes-in-python-3-7-1">Python 3.7.1 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#notable-changes-in-python-3-7-2">Python 3.7.2 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#notable-changes-in-python-3-7-6">Python 3.7.6 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.7.html#notable-changes-in-python-3-7-10">Python 3.7.10 中的重要变化</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.6.html">Python 3.6 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#new-features">新的特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#other-improvements">其他改进</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#deprecated">弃用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#removed">移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#porting-to-python-3-6">移植到Python 3.6</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-2">Python 3.6.2 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-4">Python 3.6.4 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-5">Python 3.6.5 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-7">Python 3.6.7 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-10">Python 3.6.10 中的重要变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.6.html#notable-changes-in-python-3-6-13">Python 3.6.13 中的重要变化</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.5.html">Python 3.5 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#new-features">新的特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#other-module-level-changes">其他模块级更改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#deprecated">弃用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#removed">移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#porting-to-python-3-5">移植到Python 3.5</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.5.html#notable-changes-in-python-3-5-4">Python 3.5.4 的显著变化</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.4.html">Python 3.4 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#summary-release-highlights">摘要 - 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#new-features">新的特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#cpython-implementation-changes">CPython 实现的变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#deprecated">弃用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#removed">移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#porting-to-python-3-4">移植到 Python 3.4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.4.html#changed-in-3-4-3">3.4.3 的变化</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.3.html">Python 3.3 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-405-virtual-environments">PEP 405: 虚拟环境</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-420-implicit-namespace-packages">PEP 420: 隐式命名空间包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-3118-new-memoryview-implementation-and-buffer-protocol-documentation">PEP 3118: 新的内存视图实现和缓冲协议文档</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-393-flexible-string-representation">PEP 393: 灵活的字符串表示</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-397-python-launcher-for-windows">PEP 397: 适用于Windows的Python启动器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy">PEP 3151: 重写 OS 和 IO 异常的层次结构</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-380-syntax-for-delegating-to-a-subgenerator">PEP 380: 委托给子生成器的语法</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-409-suppressing-exception-context">PEP 409: 清除异常上下文</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-414-explicit-unicode-literals">PEP 414: 显式的Unicode文本</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-3155-qualified-name-for-classes-and-functions">PEP 3155: 类和函数的限定名称</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-412-key-sharing-dictionary">PEP 412: Key-Sharing Dictionary</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-362-function-signature-object">PEP 362: 函数签名对象</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#pep-421-adding-sys-implementation">PEP 421: 添加 sys.implementation</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#using-importlib-as-the-implementation-of-import">使用 importlib 作为导入的实现</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#a-finer-grained-import-lock">A Finer-Grained Import Lock</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#builtin-functions-and-types">Builtin functions and types</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#improved-modules">改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#deprecated">弃用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.3.html#porting-to-python-3-3">移植到 Python 3.3</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.2.html">Python 3.2 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-384-defining-a-stable-abi">PEP 384: 定义稳定的ABI</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-389-argparse-command-line-parsing-module">PEP 389: Argparse 命令行解析模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-391-dictionary-based-configuration-for-logging">PEP 391: 基于字典的日志配置</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-3148-the-concurrent-futures-module">PEP 3148: <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> 模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-3147-pyc-repository-directories">PEP 3147: PYC 仓库目录</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-3149-abi-version-tagged-so-files">PEP 3149: 带有 ABI 版本标签的 .so 文件</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#pep-3333-python-web-server-gateway-interface-v1-0-1">PEP 3333: Python Web服务器网关接口v1.0.1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#multi-threading">多线程</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#unicode">Unicode</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#codecs">编解码器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#documentation">文档</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#idle">IDLE</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#code-repository">代码库</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.2.html#porting-to-python-3-2">移植到 Python 3.2</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.1.html">Python 3.1 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#pep-372-ordered-dictionaries">PEP 372: 有序字典</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#optimizations">性能优化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#idle">IDLE</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.1.html#porting-to-python-3-1">移植到 Python 3.1</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="3.0.html">Python 3.0 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#common-stumbling-blocks">常见的绊脚石</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#overview-of-syntax-changes">语法变化概述</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#changes-already-present-in-python-2-6">Changes Already Present In Python 2.6</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#library-changes">库的修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#pep-3101-a-new-approach-to-string-formatting"><strong>PEP 3101</strong>: A New Approach To String Formatting</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#changes-to-exceptions">Changes To Exceptions</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#miscellaneous-other-changes">Miscellaneous Other Changes</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#performance">性能</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="3.0.html#porting-to-python-3-0">移植到 Python 3.0</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.7.html">Python 2.7 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#the-future-for-python-2-x">Python 2.x的未来</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#changes-to-the-handling-of-deprecation-warnings">对于弃用警告处理方式的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#python-3-1-features">Python 3.1 特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372:将有序字典 添加到收藏集</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-389-the-argparse-module-for-parsing-command-lines">PEP 389:用于解析命令行的 argparse 模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-391-dictionary-based-configuration-for-logging">PEP 391: 基于字典的日志配置</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-3106-dictionary-views">PEP 3106: 字典视图</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#pep-3137-the-memoryview-object">PEP 3137: memoryview 对象</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#porting-to-python-2-7">移植到 Python 2.7</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#new-features-added-to-python-2-7-maintenance-releases">New Features Added to Python 2.7 Maintenance Releases</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.7.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.6.html">Python 2.6 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#python-3-0">Python 3.0</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#changes-to-the-development-process">开发过程的变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-343-the-with-statement">PEP 343: "with" 语句</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-366-explicit-relative-imports-from-a-main-module">PEP 366: 从主模块显式相对导入</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-370-per-user-site-packages-directory">PEP 370: 分用户的 site-packages 目录</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-371-the-multiprocessing-package">PEP 371: 多任务处理包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3101-advanced-string-formatting">PEP 3101: 高级字符串格式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3105-print-as-a-function">PEP 3105: <code class="docutils literal notranslate"><span class="pre">print</span></code> 改为函数</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3110-exception-handling-changes">PEP 3110: 异常处理的变更</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3112-byte-literals">PEP 3112: 字节字面值</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3116-new-i-o-library">PEP 3116: 新 I/O 库</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3118-revised-buffer-protocol">PEP 3118: 修改缓冲区协议</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3119-abstract-base-classes">PEP 3119: 抽象基类</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3127-integer-literal-support-and-syntax">PEP 3127: 整型文字支持和语法</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3129-class-decorators">PEP 3129: 类装饰器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#pep-3141-a-type-hierarchy-for-numbers">PEP 3141: 数字的类型层级结构</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#deprecations-and-removals">弃用和移除</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#porting-to-python-2-6">移植到Python 2.6</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.6.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.5.html">Python 2.5 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-308-conditional-expressions">PEP 308: 条件表达式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-309-partial-function-application">PEP 309: 部分功能应用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-314-metadata-for-python-software-packages-v1-1">PEP 314: Python软件包的元数据 v1.1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-328-absolute-and-relative-imports">PEP 328: 绝对导入和相对导入</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-338-executing-modules-as-scripts">PEP 338: 将模块作为脚本执行</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-341-unified-try-except-finally">PEP 341: 统一 try/except/finally</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-342-new-generator-features">PEP 342: 生成器的新特性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-343-the-with-statement">PEP 343: "with" 语句</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-352-exceptions-as-new-style-classes">PEP 352: 异常作为新型的类</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-353-using-ssize-t-as-the-index-type">PEP 353: 使用ssize_t作为索引类型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#pep-357-the-index-method">PEP 357: '__index__' 方法</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#new-improved-and-removed-modules">新增,改进和删除的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#porting-to-python-2-5">移植到Python 2.5</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.5.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.4.html">Python 2.4 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-218-built-in-set-objects">PEP 218: 内置集合对象</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-237-unifying-long-integers-and-integers">PEP 237: 统一长整数和整数</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-289-generator-expressions">PEP 289: 生成器表达式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-292-simpler-string-substitutions">PEP 292: 更简单的字符串替换</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-318-decorators-for-functions-and-methods">PEP 318: 函数和方法的装饰器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-322-reverse-iteration">PEP 322: 反向迭代</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-324-new-subprocess-module">PEP 324: 新的子进程模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-327-decimal-data-type">PEP 327: 十进制数据类型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-328-multi-line-imports">PEP 328: 多行导入</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#pep-331-locale-independent-float-string-conversions">PEP 331: Locale-Independent Float/String Conversions</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#porting-to-python-2-4">移植到 Python 2.4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.4.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.3.html">Python 2.3 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-218-a-standard-set-datatype">PEP 218: 标准集合数据类型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-255-simple-generators">PEP 255: 简单的生成器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-263-source-code-encodings">PEP 263: 源代码的字符编码格式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-273-importing-modules-from-zip-archives">PEP 273: 从ZIP压缩包导入模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-277-unicode-file-name-support-for-windows-nt">PEP 277: 针对 Windows NT 的 Unicode 文件名支持</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-278-universal-newline-support">PEP 278: 通用换行支持</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-279-enumerate">PEP 279: enumerate()</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-282-the-logging-package">PEP 282: logging 包</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-285-a-boolean-type">PEP 285: 布尔类型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-293-codec-error-handling-callbacks">PEP 293: 编解码器错误处理回调</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-301-package-index-and-metadata-for-distutils">PEP 301: Distutils的软件包索引和元数据</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-302-new-import-hooks">PEP 302: 新导入钩子</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-305-comma-separated-files">PEP 305: 逗号分隔文件</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pep-307-pickle-enhancements">PEP 307:对 pickle 的改进</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#extended-slices">扩展切片</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#other-language-changes">其他语言特性修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#new-improved-and-deprecated-modules">新增,改进和弃用的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#pymalloc-a-specialized-object-allocator">Pymalloc: A Specialized Object Allocator</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#porting-to-python-2-3">移植到 Python 2.3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.3.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.2.html">Python 2.2 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#introduction">概述</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#peps-252-and-253-type-and-class-changes">PEP 252 和 253:类型和类的修改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#pep-234-iterators">PEP 234: 迭代器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#pep-255-simple-generators">PEP 255: 简单的生成器</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#pep-237-unifying-long-integers-and-integers">PEP 237: 统一长整数和整数</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#pep-238-changing-the-division-operator">PEP 238:修改除法运算符</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#unicode-changes">Unicode 的改变</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#pep-227-nested-scopes">PEP 227: 嵌套的作用域</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#interpreter-changes-and-fixes">解释器的改变和修正</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.2.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.1.html">Python 2.1 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#introduction">概述</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-227-nested-scopes">PEP 227: 嵌套的作用域</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-236-future-directives">PEP 236: __future__ 指令</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-207-rich-comparisons">PEP 207: 富比较</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-230-warning-framework">PEP 230: 警告框架</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-229-new-build-system">PEP 229: 新的构建系统</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-205-weak-references">PEP 205: 弱引用</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-232-function-attributes">PEP 232: 函数属性</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-235-importing-modules-on-case-insensitive-platforms">PEP 235: 在大小写不敏感的平台上导入模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-217-interactive-display-hook">PEP 217: 交互模式显示钩子</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-208-new-coercion-model">PEP 208: 新的强制转换模型</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#pep-241-metadata-in-python-packages">PEP 241: Python 包中的元数据</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#new-and-improved-modules">新增和改进的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#other-changes-and-fixes">其他的改变和修正</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.1.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="2.0.html">Python 2.0 有什么新变化</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#introduction">概述</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#what-about-python-1-6">Python 1.6 将会怎样?</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#new-development-process">新开发流程</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#unicode">Unicode</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#list-comprehensions">列表推导式</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#augmented-assignment">增强赋值</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#string-methods">字符串的方法</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#garbage-collection-of-cycles">循环的垃圾回收</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#other-core-changes">其他核心变化</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#porting-to-2-0">移植 Python 2.0</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#extending-embedding-changes">扩展/嵌入更改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#distutils-making-modules-easy-to-install">Distutils:使模块易于安装</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#xml-modules">XML 模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#module-changes">模块更改</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#new-modules">新增模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#idle-improvements">IDLE 改进</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#deleted-and-deprecated-modules">删除和弃用的模块</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="2.0.html#acknowledgements">致谢</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<p>这个“更新日志”是 <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Misc/NEWS.d">Misc/NEWS.d</a> 目录下文件 <a class="reference external" href="https://pypi.org/project/blurb">构建</a> 的 HTML 版本,它包含了对当前 Python 版本进行的 <em>所有</em> 重要的更改。</p>
|
||||
<div class="toctree-wrapper compound">
|
||||
<ul>
|
||||
<li class="toctree-l1"><a class="reference internal" href="changelog.html">更新日志</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-20-final">Python 3.8.20 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-19-final">Python 3.8.19 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-18-final">Python 3.8.18 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-17-final">Python 3.8.17 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-16-final">Python 3.8.16 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-15-final">Python 3.8.15 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-14-final">Python 3.8.14 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-13-final">Python 3.8.13 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-12-final">Python 3.8.12 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-11-final">最终版本Python 3.8.11</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-10-final">Python 3.8.10 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-9-final">Python 3.8.9 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-8-final">Python 3.8.8 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-8-release-candidate-1">Python 3.8.8 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-7-final">Python 3.8.7 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-7-release-candidate-1">Python 3.8.7 release candidate 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-6-final">Python 3.8.6 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-6-release-candidate-1">Python 3.8.6 release candidate 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-5-final">Python 3.8.5 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-4-final">Python 3.8.4 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-4-release-candidate-1">Python 3.8.4 release candidate 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-3-final">Python 3.8.3 final</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-3-release-candidate-1">Python 3.8.3 release candidate 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-2-final">Python 3.8.2 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-2-release-candidate-2">Python 3.8.2 rc2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-2-release-candidate-1">Python 3.8.2 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-1-final">Python 3.8.1 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-1-release-candidate-1">Python 3.8.1 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-final">Python 3.8.0 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-release-candidate-1">Python 3.8.0 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-beta-4">Python 3.8.0 beta 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-beta-3">Python 3.8.0 beta 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-beta-2">Python 3.8.0 beta 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-beta-1">Python 3.8.0 beta 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-alpha-4">Python 3.8.0 alpha 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-alpha-3">Python 3.8.0 alpha 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-alpha-2">Python 3.8.0 alpha 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-8-0-alpha-1">Python 3.8.0 alpha 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-final">Python 3.7.0 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-release-candidate-1">Python 3.7.0 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-beta-5">Python 3.7.0 beta 5</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-beta-4">Python 3.7.0 beta 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-beta-3">Python 3.7.0 beta 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-beta-2">Python 3.7.0 beta 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-beta-1">Python 3.7.0 beta 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-alpha-4">Python 3.7.0 alpha 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-alpha-3">Python 3.7.0 alpha 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-alpha-2">Python 3.7.0 alpha 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-7-0-alpha-1">Python 3.7.0 alpha 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-6-final">Python 3.6.6 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-6-release-candidate-1">Python 3.6.6 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-5-final">Python 3.6.5 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-5-release-candidate-1">Python 3.6.5 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-4-final">Python 3.6.4 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-4-release-candidate-1">Python 3.6.4 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-3-final">Python 3.6.3 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-3-release-candidate-1">Python 3.6.3 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-2-final">Python 3.6.2 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-2-release-candidate-2">Python 3.6.2 rc2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-2-release-candidate-1">Python 3.6.2 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-1-final">Python 3.6.1 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-1-release-candidate-1">Python 3.6.1 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-final">Python 3.6.0 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-release-candidate-2">Python 3.6.0 rc2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-release-candidate-1">Python 3.6.0 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-beta-4">Python 3.6.0 beta 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-beta-3">Python 3.6.0 beta 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-beta-2">Python 3.6.0 beta 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-beta-1">Python 3.6.0 beta 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-alpha-4">Python 3.6.0 alpha 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-alpha-3">Python 3.6.0 alpha 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-alpha-2">Python 3.6.0 alpha 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-6-0-alpha-1">Python 3.6.0 alpha 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-5-final">Python 3.5.5 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-5-release-candidate-1">Python 3.5.5 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-4-final">Python 3.5.4 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-4-release-candidate-1">Python 3.5.4 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-3-final">Python 3.5.3 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-3-release-candidate-1">Python 3.5.3 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-2-final">Python 3.5.2 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-2-release-candidate-1">Python 3.5.2 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-1-final">Python 3.5.1 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-1-release-candidate-1">Python 3.5.1 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-final">Python 3.5.0 正式版</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-release-candidate-4">Python 3.5.0 rc4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-release-candidate-3">Python 3.5.0 rc3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-release-candidate-2">Python 3.5.0 rc2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-release-candidate-1">Python 3.5.0 rc1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-beta-4">Python 3.5.0 beta 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-beta-3">Python 3.5.0 beta 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-beta-2">Python 3.5.0 beta 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-beta-1">Python 3.5.0 beta 1</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-alpha-4">Python 3.5.0 alpha 4</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-alpha-3">Python 3.5.0 alpha 3</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-alpha-2">Python 3.5.0 alpha 2</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="changelog.html#python-3-5-0-alpha-1">Python 3.5.0 alpha 1</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||||
<div class="sphinxsidebarwrapper">
|
||||
<h4>上一个主题</h4>
|
||||
<p class="topless"><a href="../contents.html"
|
||||
title="上一章">Python 文档目录</a></p>
|
||||
<h4>下一个主题</h4>
|
||||
<p class="topless"><a href="3.8.html"
|
||||
title="下一章">Python 3.8 有什么新变化</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/index.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="3.8.html" title="Python 3.8 有什么新变化"
|
||||
>下一页</a> |</li>
|
||||
<li class="right" >
|
||||
<a href="../contents.html" title="Python 文档目录"
|
||||
>上一页</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="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>
|
||||
Reference in New Issue
Block a user