979 lines
129 KiB
HTML
979 lines
129 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>Python 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> |