1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>What’s New in Python 2.3 — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="What’s New in Python" href="index.html" />
34
<link rel="next" title="What’s New in Python 2.2" href="2.2.html" />
35
<link rel="prev" title="What’s New in Python 2.4" href="2.4.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="2.2.html" title="What’s New in Python 2.2"
55
accesskey="N">next</a> |</li>
57
<a href="2.4.html" title="What’s New in Python 2.4"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> »</li>
71
<div class="document">
72
<div class="documentwrapper">
73
<div class="bodywrapper">
74
<div class="body" role="main">
76
<div class="section" id="what-s-new-in-python-2-3">
77
<h1>What’s New in Python 2.3<a class="headerlink" href="#what-s-new-in-python-2-3" title="Permalink to this headline">¶</a></h1>
78
<table class="docutils field-list" frame="void" rules="none">
79
<col class="field-name" />
80
<col class="field-body" />
82
<tr class="field-odd field"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling</td>
86
<p>This article explains the new features in Python 2.3. Python 2.3 was released
88
<p>The main themes for Python 2.3 are polishing some of the features added in 2.2,
89
adding various small but useful enhancements to the core language, and expanding
90
the standard library. The new object model introduced in the previous version
91
has benefited from 18 months of bugfixes and from optimization efforts that have
92
improved the performance of new-style classes. A few new built-in functions
93
have been added such as <a class="reference internal" href="../library/functions.html#sum" title="sum"><code class="xref py py-func docutils literal"><span class="pre">sum()</span></code></a> and <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><code class="xref py py-func docutils literal"><span class="pre">enumerate()</span></code></a>. The <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal"><span class="pre">in</span></code></a>
94
operator can now be used for substring searches (e.g. <code class="docutils literal"><span class="pre">"ab"</span> <span class="pre">in</span> <span class="pre">"abc"</span></code> returns
95
<a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>).</p>
96
<p>Some of the many new library features include Boolean, set, heap, and date/time
97
data types, the ability to import modules from ZIP-format archives, metadata
98
support for the long-awaited Python catalog, an updated version of IDLE, and
99
modules for logging messages, wrapping text, parsing CSV files, processing
100
command-line options, using BerkeleyDB databases... the list of new and
101
enhanced modules is lengthy.</p>
102
<p>This article doesn’t attempt to provide a complete specification of the new
103
features, but instead provides a convenient overview. For full details, you
104
should refer to the documentation for Python 2.3, such as the Python Library
105
Reference and the Python Reference Manual. If you want to understand the
106
complete implementation and design rationale, refer to the PEP for a particular
108
<div class="section" id="pep-218-a-standard-set-datatype">
109
<h2>PEP 218: A Standard Set Datatype<a class="headerlink" href="#pep-218-a-standard-set-datatype" title="Permalink to this headline">¶</a></h2>
110
<p>The new <a class="reference internal" href="../library/sets.html#module-sets" title="sets: Implementation of sets of unique elements. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">sets</span></code></a> module contains an implementation of a set datatype. The
111
<code class="xref py py-class docutils literal"><span class="pre">Set</span></code> class is for mutable sets, sets that can have members added and
112
removed. The <code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code> class is for sets that can’t be modified,
113
and instances of <code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code> can therefore be used as dictionary keys.
114
Sets are built on top of dictionaries, so the elements within a set must be
116
<p>Here’s a simple example:</p>
117
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sets</span>
118
<span class="gp">>>> </span><span class="n">S</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</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>
119
<span class="gp">>>> </span><span class="n">S</span>
120
<span class="go">Set([1, 2, 3])</span>
121
<span class="gp">>>> </span><span class="mi">1</span> <span class="ow">in</span> <span class="n">S</span>
122
<span class="go">True</span>
123
<span class="gp">>>> </span><span class="mi">0</span> <span class="ow">in</span> <span class="n">S</span>
124
<span class="go">False</span>
125
<span class="gp">>>> </span><span class="n">S</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
126
<span class="gp">>>> </span><span class="n">S</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
127
<span class="gp">>>> </span><span class="n">S</span>
128
<span class="go">Set([1, 2, 5])</span>
129
<span class="go">>>></span>
132
<p>The union and intersection of sets can be computed with the <code class="xref py py-meth docutils literal"><span class="pre">union()</span></code> and
133
<code class="xref py py-meth docutils literal"><span class="pre">intersection()</span></code> methods; an alternative notation uses the bitwise operators
134
<code class="docutils literal"><span class="pre">&</span></code> and <code class="docutils literal"><span class="pre">|</span></code>. Mutable sets also have in-place versions of these methods,
135
<code class="xref py py-meth docutils literal"><span class="pre">union_update()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">intersection_update()</span></code>.</p>
136
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">S1</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</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>
137
<span class="gp">>>> </span><span class="n">S2</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</span><span class="p">([</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">])</span>
138
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
139
<span class="go">Set([1, 2, 3, 4, 5, 6])</span>
140
<span class="gp">>>> </span><span class="n">S1</span> <span class="o">|</span> <span class="n">S2</span> <span class="c1"># Alternative notation</span>
141
<span class="go">Set([1, 2, 3, 4, 5, 6])</span>
142
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
143
<span class="go">Set([])</span>
144
<span class="gp">>>> </span><span class="n">S1</span> <span class="o">&</span> <span class="n">S2</span> <span class="c1"># Alternative notation</span>
145
<span class="go">Set([])</span>
146
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">union_update</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
147
<span class="gp">>>> </span><span class="n">S1</span>
148
<span class="go">Set([1, 2, 3, 4, 5, 6])</span>
149
<span class="go">>>></span>
152
<p>It’s also possible to take the symmetric difference of two sets. This is the
153
set of all elements in the union that aren’t in the intersection. Another way
154
of putting it is that the symmetric difference contains all elements that are in
155
exactly one set. Again, there’s an alternative notation (<code class="docutils literal"><span class="pre">^</span></code>), and an in-
156
place version with the ungainly name <code class="xref py py-meth docutils literal"><span class="pre">symmetric_difference_update()</span></code>.</p>
157
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">S1</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</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="mi">4</span><span class="p">])</span>
158
<span class="gp">>>> </span><span class="n">S2</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">])</span>
159
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">symmetric_difference</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
160
<span class="go">Set([1, 2, 5, 6])</span>
161
<span class="gp">>>> </span><span class="n">S1</span> <span class="o">^</span> <span class="n">S2</span>
162
<span class="go">Set([1, 2, 5, 6])</span>
163
<span class="go">>>></span>
166
<p>There are also <code class="xref py py-meth docutils literal"><span class="pre">issubset()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">issuperset()</span></code> methods for checking
167
whether one set is a subset or superset of another:</p>
168
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">S1</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</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>
169
<span class="gp">>>> </span><span class="n">S2</span> <span class="o">=</span> <span class="n">sets</span><span class="o">.</span><span class="n">Set</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
170
<span class="gp">>>> </span><span class="n">S2</span><span class="o">.</span><span class="n">issubset</span><span class="p">(</span><span class="n">S1</span><span class="p">)</span>
171
<span class="go">True</span>
172
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">issubset</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
173
<span class="go">False</span>
174
<span class="gp">>>> </span><span class="n">S1</span><span class="o">.</span><span class="n">issuperset</span><span class="p">(</span><span class="n">S2</span><span class="p">)</span>
175
<span class="go">True</span>
176
<span class="go">>>></span>
179
<div class="admonition seealso">
180
<p class="first admonition-title">See also</p>
181
<dl class="last docutils">
182
<dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0218"><strong>PEP 218</strong></a> - Adding a Built-In Set Object Type</dt>
183
<dd>PEP written by Greg V. Wilson. Implemented by Greg V. Wilson, Alex Martelli, and
188
<div class="section" id="pep-255-simple-generators">
189
<span id="section-generators"></span><h2>PEP 255: Simple Generators<a class="headerlink" href="#pep-255-simple-generators" title="Permalink to this headline">¶</a></h2>
190
<p>In Python 2.2, generators were added as an optional feature, to be enabled by a
191
<code class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">generators</span></code> directive. In 2.3 generators no longer
192
need to be specially enabled, and are now always present; this means that
193
<a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> is now always a keyword. The rest of this section is a copy of
194
the description of generators from the “What’s New in Python 2.2” document; if
195
you read it back when Python 2.2 came out, you can skip the rest of this
197
<p>You’re doubtless familiar with how function calls work in Python or C. When you
198
call a function, it gets a private namespace where its local variables are
199
created. When the function reaches a <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal"><span class="pre">return</span></code></a> statement, the local
200
variables are destroyed and the resulting value is returned to the caller. A
201
later call to the same function will get a fresh new set of local variables.
202
But, what if the local variables weren’t thrown away on exiting a function?
203
What if you could later resume the function where it left off? This is what
204
generators provide; they can be thought of as resumable functions.</p>
205
<p>Here’s the simplest example of a generator function:</p>
206
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">generate_ints</span><span class="p">(</span><span class="n">N</span><span class="p">):</span>
207
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N</span><span class="p">):</span>
208
<span class="k">yield</span> <span class="n">i</span>
211
<p>A new keyword, <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a>, was introduced for generators. Any function
212
containing a <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> statement is a generator function; this is
213
detected by Python’s bytecode compiler which compiles the function specially as
215
<p>When you call a generator function, it doesn’t return a single value; instead it
216
returns a generator object that supports the iterator protocol. On executing
217
the <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> statement, the generator outputs the value of <code class="docutils literal"><span class="pre">i</span></code>,
218
similar to a <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal"><span class="pre">return</span></code></a> statement. The big difference between
219
<a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> and a <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal"><span class="pre">return</span></code></a> statement is that on reaching a
220
<a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> the generator’s state of execution is suspended and local
221
variables are preserved. On the next call to the generator’s <code class="docutils literal"><span class="pre">.next()</span></code>
222
method, the function will resume executing immediately after the
223
<a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> statement. (For complicated reasons, the <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a>
224
statement isn’t allowed inside the <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal"><span class="pre">try</span></code></a> block of a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal"><span class="pre">try</span></code></a>...<a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal"><span class="pre">finally</span></code></a> statement; read <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a> for a full explanation of the
225
interaction between <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> and exceptions.)</p>
226
<p>Here’s a sample usage of the <code class="xref py py-func docutils literal"><span class="pre">generate_ints()</span></code> generator:</p>
227
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">gen</span> <span class="o">=</span> <span class="n">generate_ints</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
228
<span class="gp">>>> </span><span class="n">gen</span>
229
<span class="go"><generator object at 0x8117f90></span>
230
<span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
231
<span class="go">0</span>
232
<span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
233
<span class="go">1</span>
234
<span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
235
<span class="go">2</span>
236
<span class="gp">>>> </span><span class="n">gen</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
237
<span class="gt">Traceback (most recent call last):</span>
238
File <span class="nb">"stdin"</span>, line <span class="m">1</span>, in <span class="n">?</span>
239
File <span class="nb">"stdin"</span>, line <span class="m">2</span>, in <span class="n">generate_ints</span>
240
<span class="gr">StopIteration</span>
243
<p>You could equally write <code class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">generate_ints(5)</span></code>, or <code class="docutils literal"><span class="pre">a,b,c</span> <span class="pre">=</span>
244
<span class="pre">generate_ints(3)</span></code>.</p>
245
<p>Inside a generator function, the <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal"><span class="pre">return</span></code></a> statement can only be used
246
without a value, and signals the end of the procession of values; afterwards the
247
generator cannot return any further values. <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal"><span class="pre">return</span></code></a> with a value, such
248
as <code class="docutils literal"><span class="pre">return</span> <span class="pre">5</span></code>, is a syntax error inside a generator function. The end of the
249
generator’s results can also be indicated by raising <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><code class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></code></a>
250
manually, or by just letting the flow of execution fall off the bottom of the
252
<p>You could achieve the effect of generators manually by writing your own class
253
and storing all the local variables of the generator as instance variables. For
254
example, returning a list of integers could be done by setting <code class="docutils literal"><span class="pre">self.count</span></code> to
255
0, and having the <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal"><span class="pre">next()</span></code></a> method increment <code class="docutils literal"><span class="pre">self.count</span></code> and return it.
256
However, for a moderately complicated generator, writing a corresponding class
257
would be much messier. <code class="file docutils literal"><span class="pre">Lib/test/test_generators.py</span></code> contains a number of
258
more interesting examples. The simplest one implements an in-order traversal of
259
a tree using generators recursively.</p>
260
<div class="highlight-python"><div class="highlight"><pre><span class="c1"># A recursive generator that generates Tree leaves in in-order.</span>
261
<span class="k">def</span> <span class="nf">inorder</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
262
<span class="k">if</span> <span class="n">t</span><span class="p">:</span>
263
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inorder</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">left</span><span class="p">):</span>
264
<span class="k">yield</span> <span class="n">x</span>
265
<span class="k">yield</span> <span class="n">t</span><span class="o">.</span><span class="n">label</span>
266
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inorder</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">right</span><span class="p">):</span>
267
<span class="k">yield</span> <span class="n">x</span>
270
<p>Two other examples in <code class="file docutils literal"><span class="pre">Lib/test/test_generators.py</span></code> produce solutions for
271
the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no
272
queen threatens another) and the Knight’s Tour (a route that takes a knight to
273
every square of an $NxN$ chessboard without visiting any square twice).</p>
274
<p>The idea of generators comes from other programming languages, especially Icon
275
(<a class="reference external" href="http://www.cs.arizona.edu/icon/">http://www.cs.arizona.edu/icon/</a>), where the idea of generators is central. In
276
Icon, every expression and function call behaves like a generator. One example
277
from “An Overview of the Icon Programming Language” at
278
<a class="reference external" href="http://www.cs.arizona.edu/icon/docs/ipd266.htm">http://www.cs.arizona.edu/icon/docs/ipd266.htm</a> gives an idea of what this looks
280
<div class="highlight-python"><div class="highlight"><pre>sentence := "Store it in the neighboring harbor"
281
if (i := find("or", sentence)) > 5 then write(i)
284
<p>In Icon the <code class="xref py py-func docutils literal"><span class="pre">find()</span></code> function returns the indexes at which the substring
285
“or” is found: 3, 23, 33. In the <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal"><span class="pre">if</span></code></a> statement, <code class="docutils literal"><span class="pre">i</span></code> is first
286
assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon
287
retries it with the second value of 23. 23 is greater than 5, so the comparison
288
now succeeds, and the code prints the value 23 to the screen.</p>
289
<p>Python doesn’t go nearly as far as Icon in adopting generators as a central
290
concept. Generators are considered part of the core Python language, but
291
learning or using them isn’t compulsory; if they don’t solve any problems that
292
you have, feel free to ignore them. One novel feature of Python’s interface as
293
compared to Icon’s is that a generator’s state is represented as a concrete
294
object (the iterator) that can be passed around to other functions or stored in
295
a data structure.</p>
296
<div class="admonition seealso">
297
<p class="first admonition-title">See also</p>
298
<dl class="last docutils">
299
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a> - Simple Generators</dt>
300
<dd>Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland. Implemented mostly
301
by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.</dd>
305
<div class="section" id="pep-263-source-code-encodings">
306
<span id="section-encodings"></span><h2>PEP 263: Source Code Encodings<a class="headerlink" href="#pep-263-source-code-encodings" title="Permalink to this headline">¶</a></h2>
307
<p>Python source files can now be declared as being in different character set
308
encodings. Encodings are declared by including a specially formatted comment in
309
the first or second line of the source file. For example, a UTF-8 file can be
311
<div class="highlight-python"><div class="highlight"><pre><span class="ch">#!/usr/bin/env python</span>
312
<span class="c1"># -*- coding: UTF-8 -*-</span>
315
<p>Without such an encoding declaration, the default encoding used is 7-bit ASCII.
316
Executing or importing modules that contain string literals with 8-bit
317
characters and have no encoding declaration will result in a
318
<a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> being signalled by Python 2.3; in 2.4 this will be a
320
<p>The encoding declaration only affects Unicode string literals, which will be
321
converted to Unicode using the specified encoding. Note that Python identifiers
322
are still restricted to ASCII characters, so you can’t have variable names that
323
use characters outside of the usual alphanumerics.</p>
324
<div class="admonition seealso">
325
<p class="first admonition-title">See also</p>
326
<dl class="last docutils">
327
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0263"><strong>PEP 263</strong></a> - Defining Python Source Code Encodings</dt>
328
<dd>Written by Marc-André Lemburg and Martin von Löwis; implemented by Suzuki Hisao
329
and Martin von Löwis.</dd>
333
<div class="section" id="pep-273-importing-modules-from-zip-archives">
334
<h2>PEP 273: Importing Modules from ZIP Archives<a class="headerlink" href="#pep-273-importing-modules-from-zip-archives" title="Permalink to this headline">¶</a></h2>
335
<p>The new <a class="reference internal" href="../library/zipimport.html#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal"><span class="pre">zipimport</span></code></a> module adds support for importing modules from a ZIP-
336
format archive. You don’t need to import the module explicitly; it will be
337
automatically imported if a ZIP archive’s filename is added to <code class="docutils literal"><span class="pre">sys.path</span></code>.
339
<div class="highlight-python"><div class="highlight"><pre>amk@nyman:~/src/python$ unzip -l /tmp/example.zip
340
Archive: /tmp/example.zip
341
Length Date Time Name
342
-------- ---- ---- ----
343
8467 11-26-02 22:30 jwzthreading.py
346
amk@nyman:~/src/python$ ./python
347
Python 2.3 (#1, Aug 1 2003, 19:54:32)
348
>>> import sys
349
>>> sys.path.insert(0, '/tmp/example.zip') # Add .zip file to front of path
350
>>> import jwzthreading
351
>>> jwzthreading.__file__
352
'/tmp/example.zip/jwzthreading.py'
356
<p>An entry in <code class="docutils literal"><span class="pre">sys.path</span></code> can now be the filename of a ZIP archive. The ZIP
357
archive can contain any kind of files, but only files named <code class="file docutils literal"><span class="pre">*.py</span></code>,
358
<code class="file docutils literal"><span class="pre">*.pyc</span></code>, or <code class="file docutils literal"><span class="pre">*.pyo</span></code> can be imported. If an archive only contains
359
<code class="file docutils literal"><span class="pre">*.py</span></code> files, Python will not attempt to modify the archive by adding the
360
corresponding <code class="file docutils literal"><span class="pre">*.pyc</span></code> file, meaning that if a ZIP archive doesn’t contain
361
<code class="file docutils literal"><span class="pre">*.pyc</span></code> files, importing may be rather slow.</p>
362
<p>A path within the archive can also be specified to only import from a
363
subdirectory; for example, the path <code class="file docutils literal"><span class="pre">/tmp/example.zip/lib/</span></code> would only
364
import from the <code class="file docutils literal"><span class="pre">lib/</span></code> subdirectory within the archive.</p>
365
<div class="admonition seealso">
366
<p class="first admonition-title">See also</p>
367
<dl class="last docutils">
368
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0273"><strong>PEP 273</strong></a> - Import Modules from Zip Archives</dt>
369
<dd>Written by James C. Ahlstrom, who also provided an implementation. Python 2.3
370
follows the specification in <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0273"><strong>PEP 273</strong></a>, but uses an implementation written by
371
Just van Rossum that uses the import hooks described in <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>. See section
372
<a class="reference internal" href="#section-pep302"><span>PEP 302: New Import Hooks</span></a> for a description of the new import hooks.</dd>
376
<div class="section" id="pep-277-unicode-file-name-support-for-windows-nt">
377
<h2>PEP 277: Unicode file name support for Windows NT<a class="headerlink" href="#pep-277-unicode-file-name-support-for-windows-nt" title="Permalink to this headline">¶</a></h2>
378
<p>On Windows NT, 2000, and XP, the system stores file names as Unicode strings.
379
Traditionally, Python has represented file names as byte strings, which is
380
inadequate because it renders some file names inaccessible.</p>
381
<p>Python now allows using arbitrary Unicode strings (within the limitations of the
382
file system) for all functions that expect file names, most notably the
383
<a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> built-in function. If a Unicode string is passed to
384
<a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></code></a>, Python now returns a list of Unicode strings. A new
385
function, <a class="reference internal" href="../library/os.html#os.getcwdu" title="os.getcwdu"><code class="xref py py-func docutils literal"><span class="pre">os.getcwdu()</span></code></a>, returns the current directory as a Unicode string.</p>
386
<p>Byte strings still work as file names, and on Windows Python will transparently
387
convert them to Unicode using the <code class="docutils literal"><span class="pre">mbcs</span></code> encoding.</p>
388
<p>Other systems also allow Unicode strings as file names but convert them to byte
389
strings before passing them to the system, which can cause a <a class="reference internal" href="../library/exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><code class="xref py py-exc docutils literal"><span class="pre">UnicodeError</span></code></a>
390
to be raised. Applications can test whether arbitrary Unicode strings are
391
supported as file names by checking <a class="reference internal" href="../library/os.path.html#os.path.supports_unicode_filenames" title="os.path.supports_unicode_filenames"><code class="xref py py-attr docutils literal"><span class="pre">os.path.supports_unicode_filenames</span></code></a>,
393
<p>Under MacOS, <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></code></a> may now return Unicode filenames.</p>
394
<div class="admonition seealso">
395
<p class="first admonition-title">See also</p>
396
<dl class="last docutils">
397
<dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0277"><strong>PEP 277</strong></a> - Unicode file name support for Windows NT</dt>
398
<dd>Written by Neil Hodgson; implemented by Neil Hodgson, Martin von Löwis, and Mark
403
<div class="section" id="pep-278-universal-newline-support">
404
<span id="index-8"></span><h2>PEP 278: Universal Newline Support<a class="headerlink" href="#pep-278-universal-newline-support" title="Permalink to this headline">¶</a></h2>
405
<p>The three major operating systems used today are Microsoft Windows, Apple’s
406
Macintosh OS, and the various Unix derivatives. A minor irritation of cross-
407
platform work is that these three platforms all use different characters to
408
mark the ends of lines in text files. Unix uses the linefeed (ASCII character
409
10), MacOS uses the carriage return (ASCII character 13), and Windows uses a
410
two-character sequence of a carriage return plus a newline.</p>
411
<p>Python’s file objects can now support end of line conventions other than the
412
one followed by the platform on which Python is running. Opening a file with
413
the mode <code class="docutils literal"><span class="pre">'U'</span></code> or <code class="docutils literal"><span class="pre">'rU'</span></code> will open a file for reading in <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal
414
newlines</span></a> mode. All three line ending conventions will be translated to a
415
<code class="docutils literal"><span class="pre">'\n'</span></code> in the strings returned by the various file methods such as
416
<code class="xref py py-meth docutils literal"><span class="pre">read()</span></code> and <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"><span class="pre">readline()</span></code></a>.</p>
417
<p>Universal newline support is also used when importing modules and when executing
418
a file with the <a class="reference internal" href="../library/functions.html#execfile" title="execfile"><code class="xref py py-func docutils literal"><span class="pre">execfile()</span></code></a> function. This means that Python modules can
419
be shared between all three operating systems without needing to convert the
421
<p>This feature can be disabled when compiling Python by specifying the
422
<code class="xref std std-option docutils literal"><span class="pre">--without-universal-newlines</span></code> switch when running Python’s
423
<strong class="program">configure</strong> script.</p>
424
<div class="admonition seealso">
425
<p class="first admonition-title">See also</p>
426
<dl class="last docutils">
427
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0278"><strong>PEP 278</strong></a> - Universal Newline Support</dt>
428
<dd>Written and implemented by Jack Jansen.</dd>
432
<div class="section" id="pep-279-enumerate">
433
<span id="section-enumerate"></span><h2>PEP 279: enumerate()<a class="headerlink" href="#pep-279-enumerate" title="Permalink to this headline">¶</a></h2>
434
<p>A new built-in function, <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><code class="xref py py-func docutils literal"><span class="pre">enumerate()</span></code></a>, will make certain loops a bit
435
clearer. <code class="docutils literal"><span class="pre">enumerate(thing)</span></code>, where <em>thing</em> is either an iterator or a
436
sequence, returns a iterator that will return <code class="docutils literal"><span class="pre">(0,</span> <span class="pre">thing[0])</span></code>, <code class="docutils literal"><span class="pre">(1,</span>
437
<span class="pre">thing[1])</span></code>, <code class="docutils literal"><span class="pre">(2,</span> <span class="pre">thing[2])</span></code>, and so forth.</p>
438
<p>A common idiom to change every element of a list looks like this:</p>
439
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">L</span><span class="p">)):</span>
440
<span class="n">item</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
441
<span class="c1"># ... compute some result based on item ...</span>
442
<span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>
445
<p>This can be rewritten using <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><code class="xref py py-func docutils literal"><span class="pre">enumerate()</span></code></a> as:</p>
446
<div class="highlight-python"><div class="highlight"><pre><span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">L</span><span class="p">):</span>
447
<span class="c1"># ... compute some result based on item ...</span>
448
<span class="n">L</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span>
451
<div class="admonition seealso">
452
<p class="first admonition-title">See also</p>
453
<dl class="last docutils">
454
<dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0279"><strong>PEP 279</strong></a> - The enumerate() built-in function</dt>
455
<dd>Written and implemented by Raymond D. Hettinger.</dd>
459
<div class="section" id="pep-282-the-logging-package">
460
<h2>PEP 282: The logging Package<a class="headerlink" href="#pep-282-the-logging-package" title="Permalink to this headline">¶</a></h2>
461
<p>A standard package for writing logs, <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a>, has been added to Python
462
2.3. It provides a powerful and flexible mechanism for generating logging
463
output which can then be filtered and processed in various ways. A
464
configuration file written in a standard format can be used to control the
465
logging behavior of a program. Python includes handlers that will write log
466
records to standard error or to a file or socket, send them to the system log,
467
or even e-mail them to a particular address; of course, it’s also possible to
468
write your own handler classes.</p>
469
<p>The <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> class is the primary class. Most application code will deal
470
with one or more <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> objects, each one used by a particular
471
subsystem of the application. Each <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> is identified by a name, and
472
names are organized into a hierarchy using <code class="docutils literal"><span class="pre">.</span></code> as the component separator.
473
For example, you might have <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> instances named <code class="docutils literal"><span class="pre">server</span></code>,
474
<code class="docutils literal"><span class="pre">server.auth</span></code> and <code class="docutils literal"><span class="pre">server.network</span></code>. The latter two instances are below
475
<code class="docutils literal"><span class="pre">server</span></code> in the hierarchy. This means that if you turn up the verbosity for
476
<code class="docutils literal"><span class="pre">server</span></code> or direct <code class="docutils literal"><span class="pre">server</span></code> messages to a different handler, the changes
477
will also apply to records logged to <code class="docutils literal"><span class="pre">server.auth</span></code> and <code class="docutils literal"><span class="pre">server.network</span></code>.
478
There’s also a root <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> that’s the parent of all other loggers.</p>
479
<p>For simple uses, the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> package contains some convenience functions
480
that always use the root log:</p>
481
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
483
<span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'Debugging information'</span><span class="p">)</span>
484
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Informational message'</span><span class="p">)</span>
485
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">'Warning:config file </span><span class="si">%s</span><span class="s1"> not found'</span><span class="p">,</span> <span class="s1">'server.conf'</span><span class="p">)</span>
486
<span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">'Error occurred'</span><span class="p">)</span>
487
<span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s1">'Critical error -- shutting down'</span><span class="p">)</span>
490
<p>This produces the following output:</p>
491
<div class="highlight-python"><div class="highlight"><pre>WARNING:root:Warning:config file server.conf not found
492
ERROR:root:Error occurred
493
CRITICAL:root:Critical error -- shutting down
496
<p>In the default configuration, informational and debugging messages are
497
suppressed and the output is sent to standard error. You can enable the display
498
of informational and debugging messages by calling the <code class="xref py py-meth docutils literal"><span class="pre">setLevel()</span></code> method
499
on the root logger.</p>
500
<p>Notice the <code class="xref py py-func docutils literal"><span class="pre">warning()</span></code> call’s use of string formatting operators; all of the
501
functions for logging messages take the arguments <code class="docutils literal"><span class="pre">(msg,</span> <span class="pre">arg1,</span> <span class="pre">arg2,</span> <span class="pre">...)</span></code> and
502
log the string resulting from <code class="docutils literal"><span class="pre">msg</span> <span class="pre">%</span> <span class="pre">(arg1,</span> <span class="pre">arg2,</span> <span class="pre">...)</span></code>.</p>
503
<p>There’s also an <code class="xref py py-func docutils literal"><span class="pre">exception()</span></code> function that records the most recent
504
traceback. Any of the other functions will also record the traceback if you
505
specify a true value for the keyword argument <em>exc_info</em>.</p>
506
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
507
<span class="k">try</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
508
<span class="k">except</span><span class="p">:</span> <span class="n">logging</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="s1">'Problem recorded'</span><span class="p">)</span>
510
<span class="n">f</span><span class="p">()</span>
513
<p>This produces the following output:</p>
514
<div class="highlight-python"><div class="highlight"><pre>ERROR:root:Problem recorded
515
Traceback (most recent call last):
516
File "t.py", line 6, in f
518
ZeroDivisionError: integer division or modulo by zero
521
<p>Slightly more advanced programs will use a logger other than the root logger.
522
The <code class="xref py py-func docutils literal"><span class="pre">getLogger(name)()</span></code> function is used to get a particular log, creating
523
it if it doesn’t exist yet. <code class="xref py py-func docutils literal"><span class="pre">getLogger(None)()</span></code> returns the root logger.</p>
524
<div class="highlight-python"><div class="highlight"><pre>log = logging.getLogger('server')
526
log.info('Listening on port %i', port)
528
log.critical('Disk full')
532
<p>Log records are usually propagated up the hierarchy, so a message logged to
533
<code class="docutils literal"><span class="pre">server.auth</span></code> is also seen by <code class="docutils literal"><span class="pre">server</span></code> and <code class="docutils literal"><span class="pre">root</span></code>, but a <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code>
534
can prevent this by setting its <code class="xref py py-attr docutils literal"><span class="pre">propagate</span></code> attribute to <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>.</p>
535
<p>There are more classes provided by the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> package that can be
536
customized. When a <code class="xref py py-class docutils literal"><span class="pre">Logger</span></code> instance is told to log a message, it
537
creates a <code class="xref py py-class docutils literal"><span class="pre">LogRecord</span></code> instance that is sent to any number of different
538
<code class="xref py py-class docutils literal"><span class="pre">Handler</span></code> instances. Loggers and handlers can also have an attached list
539
of filters, and each filter can cause the <code class="xref py py-class docutils literal"><span class="pre">LogRecord</span></code> to be ignored or
540
can modify the record before passing it along. When they’re finally output,
541
<code class="xref py py-class docutils literal"><span class="pre">LogRecord</span></code> instances are converted to text by a <code class="xref py py-class docutils literal"><span class="pre">Formatter</span></code>
542
class. All of these classes can be replaced by your own specially-written
544
<p>With all of these features the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> package should provide enough
545
flexibility for even the most complicated applications. This is only an
546
incomplete overview of its features, so please see the package’s reference
547
documentation for all of the details. Reading <span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0282"><strong>PEP 282</strong></a> will also be helpful.</p>
548
<div class="admonition seealso">
549
<p class="first admonition-title">See also</p>
550
<dl class="last docutils">
551
<dt><span class="target" id="index-12"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0282"><strong>PEP 282</strong></a> - A Logging System</dt>
552
<dd>Written by Vinay Sajip and Trent Mick; implemented by Vinay Sajip.</dd>
556
<div class="section" id="pep-285-a-boolean-type">
557
<span id="section-bool"></span><h2>PEP 285: A Boolean Type<a class="headerlink" href="#pep-285-a-boolean-type" title="Permalink to this headline">¶</a></h2>
558
<p>A Boolean type was added to Python 2.3. Two new constants were added to the
559
<a class="reference internal" href="../library/__builtin__.html#module-__builtin__" title="__builtin__: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal"><span class="pre">__builtin__</span></code></a> module, <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> and <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>. (<a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> and
560
<a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> constants were added to the built-ins in Python 2.2.1, but the
561
2.2.1 versions are simply set to integer values of 1 and 0 and aren’t a
563
<p>The type object for this new type is named <a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-class docutils literal"><span class="pre">bool</span></code></a>; the constructor for it
564
takes any Python value and converts it to <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> or <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>.</p>
565
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">bool</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
566
<span class="go">True</span>
567
<span class="gp">>>> </span><span class="nb">bool</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
568
<span class="go">False</span>
569
<span class="gp">>>> </span><span class="nb">bool</span><span class="p">([])</span>
570
<span class="go">False</span>
571
<span class="gp">>>> </span><span class="nb">bool</span><span class="p">(</span> <span class="p">(</span><span class="mi">1</span><span class="p">,)</span> <span class="p">)</span>
572
<span class="go">True</span>
575
<p>Most of the standard library modules and built-in functions have been changed to
577
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">obj</span> <span class="o">=</span> <span class="p">[]</span>
578
<span class="gp">>>> </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">'append'</span><span class="p">)</span>
579
<span class="go">True</span>
580
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span>
581
<span class="go">True</span>
582
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
583
<span class="go">False</span>
586
<p>Python’s Booleans were added with the primary goal of making code clearer. For
587
example, if you’re reading a function and encounter the statement <code class="docutils literal"><span class="pre">return</span> <span class="pre">1</span></code>,
588
you might wonder whether the <code class="docutils literal"><span class="pre">1</span></code> represents a Boolean truth value, an index,
589
or a coefficient that multiplies some other quantity. If the statement is
590
<code class="docutils literal"><span class="pre">return</span> <span class="pre">True</span></code>, however, the meaning of the return value is quite clear.</p>
591
<p>Python’s Booleans were <em>not</em> added for the sake of strict type-checking. A very
592
strict language such as Pascal would also prevent you performing arithmetic with
593
Booleans, and would require that the expression in an <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal"><span class="pre">if</span></code></a> statement
594
always evaluate to a Boolean result. Python is not this strict and never will
595
be, as <span class="target" id="index-13"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0285"><strong>PEP 285</strong></a> explicitly says. This means you can still use any expression
596
in an <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal"><span class="pre">if</span></code></a> statement, even ones that evaluate to a list or tuple or
597
some random object. The Boolean type is a subclass of the <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal"><span class="pre">int</span></code></a> class so
598
that arithmetic using a Boolean still works.</p>
599
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="bp">True</span> <span class="o">+</span> <span class="mi">1</span>
600
<span class="go">2</span>
601
<span class="gp">>>> </span><span class="bp">False</span> <span class="o">+</span> <span class="mi">1</span>
602
<span class="go">1</span>
603
<span class="gp">>>> </span><span class="bp">False</span> <span class="o">*</span> <span class="mi">75</span>
604
<span class="go">0</span>
605
<span class="gp">>>> </span><span class="bp">True</span> <span class="o">*</span> <span class="mi">75</span>
606
<span class="go">75</span>
609
<p>To sum up <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> and <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> in a sentence: they’re alternative
610
ways to spell the integer values 1 and 0, with the single difference that
611
<a class="reference internal" href="../library/functions.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a> and <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> return the strings <code class="docutils literal"><span class="pre">'True'</span></code> and <code class="docutils literal"><span class="pre">'False'</span></code>
612
instead of <code class="docutils literal"><span class="pre">'1'</span></code> and <code class="docutils literal"><span class="pre">'0'</span></code>.</p>
613
<div class="admonition seealso">
614
<p class="first admonition-title">See also</p>
615
<dl class="last docutils">
616
<dt><span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0285"><strong>PEP 285</strong></a> - Adding a bool type</dt>
617
<dd>Written and implemented by GvR.</dd>
621
<div class="section" id="pep-293-codec-error-handling-callbacks">
622
<h2>PEP 293: Codec Error Handling Callbacks<a class="headerlink" href="#pep-293-codec-error-handling-callbacks" title="Permalink to this headline">¶</a></h2>
623
<p>When encoding a Unicode string into a byte string, unencodable characters may be
624
encountered. So far, Python has allowed specifying the error processing as
625
either “strict” (raising <a class="reference internal" href="../library/exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><code class="xref py py-exc docutils literal"><span class="pre">UnicodeError</span></code></a>), “ignore” (skipping the
626
character), or “replace” (using a question mark in the output string), with
627
“strict” being the default behavior. It may be desirable to specify alternative
628
processing of such errors, such as inserting an XML character reference or HTML
629
entity reference into the converted string.</p>
630
<p>Python now has a flexible framework to add different processing strategies. New
631
error handlers can be added with <a class="reference internal" href="../library/codecs.html#codecs.register_error" title="codecs.register_error"><code class="xref py py-func docutils literal"><span class="pre">codecs.register_error()</span></code></a>, and codecs then
632
can access the error handler with <a class="reference internal" href="../library/codecs.html#codecs.lookup_error" title="codecs.lookup_error"><code class="xref py py-func docutils literal"><span class="pre">codecs.lookup_error()</span></code></a>. An equivalent C
633
API has been added for codecs written in C. The error handler gets the necessary
634
state information such as the string being converted, the position in the string
635
where the error was detected, and the target encoding. The handler can then
636
either raise an exception or return a replacement string.</p>
637
<p>Two additional error handlers have been implemented using this framework:
638
“backslashreplace” uses Python backslash quoting to represent unencodable
639
characters and “xmlcharrefreplace” emits XML character references.</p>
640
<div class="admonition seealso">
641
<p class="first admonition-title">See also</p>
642
<dl class="last docutils">
643
<dt><span class="target" id="index-15"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0293"><strong>PEP 293</strong></a> - Codec Error Handling Callbacks</dt>
644
<dd>Written and implemented by Walter Dörwald.</dd>
648
<div class="section" id="pep-301-package-index-and-metadata-for-distutils">
649
<span id="section-pep301"></span><h2>PEP 301: Package Index and Metadata for Distutils<a class="headerlink" href="#pep-301-package-index-and-metadata-for-distutils" title="Permalink to this headline">¶</a></h2>
650
<p>Support for the long-requested Python catalog makes its first appearance in 2.3.</p>
651
<p>The heart of the catalog is the new Distutils <strong class="command">register</strong> command.
652
Running <code class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">register</span></code> will collect the metadata describing a
653
package, such as its name, version, maintainer, description, &c., and send it to
654
a central catalog server. The resulting catalog is available from
655
<a class="reference external" href="https://pypi.python.org/pypi">https://pypi.python.org/pypi</a>.</p>
656
<p>To make the catalog a bit more useful, a new optional <em>classifiers</em> keyword
657
argument has been added to the Distutils <code class="xref py py-func docutils literal"><span class="pre">setup()</span></code> function. A list of
658
<a class="reference external" href="http://catb.org/~esr/trove/">Trove</a>-style strings can be supplied to help
659
classify the software.</p>
660
<p>Here’s an example <code class="file docutils literal"><span class="pre">setup.py</span></code> with classifiers, written to be compatible
661
with older versions of the Distutils:</p>
662
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">distutils</span> <span class="kn">import</span> <span class="n">core</span>
663
<span class="n">kw</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s2">"Quixote"</span><span class="p">,</span>
664
<span class="s1">'version'</span><span class="p">:</span> <span class="s2">"0.5.1"</span><span class="p">,</span>
665
<span class="s1">'description'</span><span class="p">:</span> <span class="s2">"A highly Pythonic Web application framework"</span><span class="p">,</span>
666
<span class="c1"># ...</span>
667
<span class="p">}</span>
669
<span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">core</span><span class="p">,</span> <span class="s1">'setup_keywords'</span><span class="p">)</span> <span class="ow">and</span>
670
<span class="s1">'classifiers'</span> <span class="ow">in</span> <span class="n">core</span><span class="o">.</span><span class="n">setup_keywords</span><span class="p">):</span>
671
<span class="n">kw</span><span class="p">[</span><span class="s1">'classifiers'</span><span class="p">]</span> <span class="o">=</span> \
672
<span class="p">[</span><span class="s1">'Topic :: Internet :: WWW/HTTP :: Dynamic Content'</span><span class="p">,</span>
673
<span class="s1">'Environment :: No Input/Output (Daemon)'</span><span class="p">,</span>
674
<span class="s1">'Intended Audience :: Developers'</span><span class="p">],</span>
676
<span class="n">core</span><span class="o">.</span><span class="n">setup</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">)</span>
679
<p>The full list of classifiers can be obtained by running <code class="docutils literal"><span class="pre">python</span> <span class="pre">setup.py</span>
680
<span class="pre">register</span> <span class="pre">--list-classifiers</span></code>.</p>
681
<div class="admonition seealso">
682
<p class="first admonition-title">See also</p>
683
<dl class="last docutils">
684
<dt><span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0301"><strong>PEP 301</strong></a> - Package Index and Metadata for Distutils</dt>
685
<dd>Written and implemented by Richard Jones.</dd>
689
<div class="section" id="pep-302-new-import-hooks">
690
<span id="section-pep302"></span><h2>PEP 302: New Import Hooks<a class="headerlink" href="#pep-302-new-import-hooks" title="Permalink to this headline">¶</a></h2>
691
<p>While it’s been possible to write custom import hooks ever since the
692
<code class="xref py py-mod docutils literal"><span class="pre">ihooks</span></code> module was introduced in Python 1.3, no one has ever been really
693
happy with it because writing new import hooks is difficult and messy. There
694
have been various proposed alternatives such as the <a class="reference internal" href="../library/imputil.html#module-imputil" title="imputil: Manage and augment the import process. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">imputil</span></code></a> and <code class="xref py py-mod docutils literal"><span class="pre">iu</span></code>
695
modules, but none of them has ever gained much acceptance, and none of them were
696
easily usable from C code.</p>
697
<p><span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> borrows ideas from its predecessors, especially from Gordon
698
McMillan’s <code class="xref py py-mod docutils literal"><span class="pre">iu</span></code> module. Three new items are added to the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal"><span class="pre">sys</span></code></a>
701
<li><code class="docutils literal"><span class="pre">sys.path_hooks</span></code> is a list of callable objects; most often they’ll be
702
classes. Each callable takes a string containing a path and either returns an
703
importer object that will handle imports from this path or raises an
704
<a class="reference internal" href="../library/exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><code class="xref py py-exc docutils literal"><span class="pre">ImportError</span></code></a> exception if it can’t handle this path.</li>
705
<li><code class="docutils literal"><span class="pre">sys.path_importer_cache</span></code> caches importer objects for each path, so
706
<code class="docutils literal"><span class="pre">sys.path_hooks</span></code> will only need to be traversed once for each path.</li>
707
<li><code class="docutils literal"><span class="pre">sys.meta_path</span></code> is a list of importer objects that will be traversed before
708
<code class="docutils literal"><span class="pre">sys.path</span></code> is checked. This list is initially empty, but user code can add
709
objects to it. Additional built-in and frozen modules can be imported by an
710
object added to this list.</li>
712
<p>Importer objects must have a single method, <code class="xref py py-meth docutils literal"><span class="pre">find_module(fullname,</span>
713
<span class="pre">path=None)()</span></code>. <em>fullname</em> will be a module or package name, e.g. <code class="docutils literal"><span class="pre">string</span></code> or
714
<code class="docutils literal"><span class="pre">distutils.core</span></code>. <code class="xref py py-meth docutils literal"><span class="pre">find_module()</span></code> must return a loader object that has a
715
single method, <code class="xref py py-meth docutils literal"><span class="pre">load_module(fullname)()</span></code>, that creates and returns the
716
corresponding module object.</p>
717
<p>Pseudo-code for Python’s new import logic, therefore, looks something like this
718
(simplified a bit; see <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for the full details):</p>
719
<div class="highlight-python"><div class="highlight"><pre>for mp in sys.meta_path:
720
loader = mp(fullname)
721
if loader is not None:
722
<module> = loader.load_module(fullname)
724
for path in sys.path:
725
for hook in sys.path_hooks:
727
importer = hook(path)
729
# ImportError, so try the other path hooks
732
loader = importer.find_module(fullname)
733
<module> = loader.load_module(fullname)
739
<div class="admonition seealso">
740
<p class="first admonition-title">See also</p>
741
<dl class="last docutils">
742
<dt><span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> - New Import Hooks</dt>
743
<dd>Written by Just van Rossum and Paul Moore. Implemented by Just van Rossum.</dd>
747
<div class="section" id="pep-305-comma-separated-files">
748
<span id="section-pep305"></span><h2>PEP 305: Comma-separated Files<a class="headerlink" href="#pep-305-comma-separated-files" title="Permalink to this headline">¶</a></h2>
749
<p>Comma-separated files are a format frequently used for exporting data from
750
databases and spreadsheets. Python 2.3 adds a parser for comma-separated files.</p>
751
<p>Comma-separated format is deceptively simple at first glance:</p>
752
<div class="highlight-python"><div class="highlight"><pre><span class="n">Costs</span><span class="p">,</span><span class="mi">150</span><span class="p">,</span><span class="mi">200</span><span class="p">,</span><span class="mf">3.95</span>
755
<p>Read a line and call <code class="docutils literal"><span class="pre">line.split(',')</span></code>: what could be simpler? But toss in
756
string data that can contain commas, and things get more complicated:</p>
757
<div class="highlight-python"><div class="highlight"><pre><span class="s2">"Costs"</span><span class="p">,</span><span class="mi">150</span><span class="p">,</span><span class="mi">200</span><span class="p">,</span><span class="mf">3.95</span><span class="p">,</span><span class="s2">"Includes taxes, shipping, and sundry items"</span>
760
<p>A big ugly regular expression can parse this, but using the new <a class="reference internal" href="../library/csv.html#module-csv" title="csv: Write and read tabular data to and from delimited files."><code class="xref py py-mod docutils literal"><span class="pre">csv</span></code></a>
761
package is much simpler:</p>
762
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">csv</span>
764
<span class="nb">input</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'datafile'</span><span class="p">,</span> <span class="s1">'rb'</span><span class="p">)</span>
765
<span class="n">reader</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
766
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
767
<span class="k">print</span> <span class="n">line</span>
770
<p>The <code class="xref py py-func docutils literal"><span class="pre">reader()</span></code> function takes a number of different options. The field
771
separator isn’t limited to the comma and can be changed to any character, and so
772
can the quoting and line-ending characters.</p>
773
<p>Different dialects of comma-separated files can be defined and registered;
774
currently there are two dialects, both used by Microsoft Excel. A separate
775
<a class="reference internal" href="../library/csv.html#csv.writer" title="csv.writer"><code class="xref py py-class docutils literal"><span class="pre">csv.writer</span></code></a> class will generate comma-separated files from a succession
776
of tuples or lists, quoting strings that contain the delimiter.</p>
777
<div class="admonition seealso">
778
<p class="first admonition-title">See also</p>
779
<dl class="last docutils">
780
<dt><span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0305"><strong>PEP 305</strong></a> - CSV File API</dt>
781
<dd>Written and implemented by Kevin Altis, Dave Cole, Andrew McNamara, Skip
782
Montanaro, Cliff Wells.</dd>
786
<div class="section" id="pep-307-pickle-enhancements">
787
<span id="section-pep307"></span><h2>PEP 307: Pickle Enhancements<a class="headerlink" href="#pep-307-pickle-enhancements" title="Permalink to this headline">¶</a></h2>
788
<p>The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> and <a class="reference internal" href="../library/pickle.html#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><code class="xref py py-mod docutils literal"><span class="pre">cPickle</span></code></a> modules received some attention during the
789
2.3 development cycle. In 2.2, new-style classes could be pickled without
790
difficulty, but they weren’t pickled very compactly; <span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0307"><strong>PEP 307</strong></a> quotes a trivial
791
example where a new-style class results in a pickled string three times longer
792
than that for a classic class.</p>
793
<p>The solution was to invent a new pickle protocol. The <a class="reference internal" href="../library/pickle.html#pickle.dumps" title="pickle.dumps"><code class="xref py py-func docutils literal"><span class="pre">pickle.dumps()</span></code></a>
794
function has supported a text-or-binary flag for a long time. In 2.3, this
795
flag is redefined from a Boolean to an integer: 0 is the old text-mode pickle
796
format, 1 is the old binary format, and now 2 is a new 2.3-specific format. A
797
new constant, <a class="reference internal" href="../library/pickle.html#pickle.HIGHEST_PROTOCOL" title="pickle.HIGHEST_PROTOCOL"><code class="xref py py-const docutils literal"><span class="pre">pickle.HIGHEST_PROTOCOL</span></code></a>, can be used to select the
798
fanciest protocol available.</p>
799
<p>Unpickling is no longer considered a safe operation. 2.2’s <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a>
800
provided hooks for trying to prevent unsafe classes from being unpickled
801
(specifically, a <code class="xref py py-attr docutils literal"><span class="pre">__safe_for_unpickling__</span></code> attribute), but none of this
802
code was ever audited and therefore it’s all been ripped out in 2.3. You should
803
not unpickle untrusted data in any version of Python.</p>
804
<p>To reduce the pickling overhead for new-style classes, a new interface for
805
customizing pickling was added using three special methods:
806
<a class="reference internal" href="../library/pickle.html#object.__getstate__" title="object.__getstate__"><code class="xref py py-meth docutils literal"><span class="pre">__getstate__()</span></code></a>, <a class="reference internal" href="../library/pickle.html#object.__setstate__" title="object.__setstate__"><code class="xref py py-meth docutils literal"><span class="pre">__setstate__()</span></code></a>, and <a class="reference internal" href="../library/pickle.html#object.__getnewargs__" title="object.__getnewargs__"><code class="xref py py-meth docutils literal"><span class="pre">__getnewargs__()</span></code></a>. Consult
807
<span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0307"><strong>PEP 307</strong></a> for the full semantics of these methods.</p>
808
<p>As a way to compress pickles yet further, it’s now possible to use integer codes
809
instead of long strings to identify pickled classes. The Python Software
810
Foundation will maintain a list of standardized codes; there’s also a range of
811
codes for private use. Currently no codes have been specified.</p>
812
<div class="admonition seealso">
813
<p class="first admonition-title">See also</p>
814
<dl class="last docutils">
815
<dt><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0307"><strong>PEP 307</strong></a> - Extensions to the pickle protocol</dt>
816
<dd>Written and implemented by Guido van Rossum and Tim Peters.</dd>
820
<div class="section" id="extended-slices">
821
<span id="section-slices"></span><h2>Extended Slices<a class="headerlink" href="#extended-slices" title="Permalink to this headline">¶</a></h2>
822
<p>Ever since Python 1.4, the slicing syntax has supported an optional third “step”
823
or “stride” argument. For example, these are all legal Python syntax:
824
<code class="docutils literal"><span class="pre">L[1:10:2]</span></code>, <code class="docutils literal"><span class="pre">L[:-1:1]</span></code>, <code class="docutils literal"><span class="pre">L[::-1]</span></code>. This was added to Python at the
825
request of the developers of Numerical Python, which uses the third argument
826
extensively. However, Python’s built-in list, tuple, and string sequence types
827
have never supported this feature, raising a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> if you tried it.
828
Michael Hudson contributed a patch to fix this shortcoming.</p>
829
<p>For example, you can now easily extract the elements of a list that have even
831
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">L</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
832
<span class="gp">>>> </span><span class="n">L</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
833
<span class="go">[0, 2, 4, 6, 8]</span>
836
<p>Negative values also work to make a copy of the same list in reverse order:</p>
837
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">L</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
838
<span class="go">[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]</span>
841
<p>This also works for tuples, arrays, and strings:</p>
842
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span><span class="o">=</span><span class="s1">'abcd'</span>
843
<span class="gp">>>> </span><span class="n">s</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
844
<span class="go">'ac'</span>
845
<span class="gp">>>> </span><span class="n">s</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
846
<span class="go">'dcba'</span>
849
<p>If you have a mutable sequence such as a list or an array you can assign to or
850
delete an extended slice, but there are some differences between assignment to
851
extended and regular slices. Assignment to a regular slice can be used to
852
change the length of the sequence:</p>
853
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
854
<span class="gp">>>> </span><span class="n">a</span>
855
<span class="go">[0, 1, 2]</span>
856
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
857
<span class="gp">>>> </span><span class="n">a</span>
858
<span class="go">[0, 4, 5, 6]</span>
861
<p>Extended slices aren’t this flexible. When assigning to an extended slice, the
862
list on the right hand side of the statement must contain the same number of
863
items as the slice it is replacing:</p>
864
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
865
<span class="gp">>>> </span><span class="n">a</span>
866
<span class="go">[0, 1, 2, 3]</span>
867
<span class="gp">>>> </span><span class="n">a</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
868
<span class="go">[0, 2]</span>
869
<span class="gp">>>> </span><span class="n">a</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">]</span>
870
<span class="gp">>>> </span><span class="n">a</span>
871
<span class="go">[0, 1, -1, 3]</span>
872
<span class="gp">>>> </span><span class="n">a</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span>
873
<span class="gt">Traceback (most recent call last):</span>
874
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
875
<span class="gr">ValueError</span>: <span class="n">attempt to assign sequence of size 3 to extended slice of size 2</span>
878
<p>Deletion is more straightforward:</p>
879
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
880
<span class="gp">>>> </span><span class="n">a</span>
881
<span class="go">[0, 1, 2, 3]</span>
882
<span class="gp">>>> </span><span class="n">a</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
883
<span class="go">[0, 2]</span>
884
<span class="gp">>>> </span><span class="k">del</span> <span class="n">a</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
885
<span class="gp">>>> </span><span class="n">a</span>
886
<span class="go">[1, 3]</span>
889
<p>One can also now pass slice objects to the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a> methods of the
890
built-in sequences:</p>
891
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">__getitem__</span><span class="p">(</span><span class="nb">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
892
<span class="go">[0, 2, 4]</span>
895
<p>Or use slice objects directly in subscripts:</p>
896
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)[</span><span class="nb">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>
897
<span class="go">[0, 2, 4]</span>
900
<p>To simplify implementing sequences that support extended slicing, slice objects
901
now have a method <code class="xref py py-meth docutils literal"><span class="pre">indices(length)()</span></code> which, given the length of a sequence,
902
returns a <code class="docutils literal"><span class="pre">(start,</span> <span class="pre">stop,</span> <span class="pre">step)</span></code> tuple that can be passed directly to
903
<a class="reference internal" href="../library/functions.html#range" title="range"><code class="xref py py-func docutils literal"><span class="pre">range()</span></code></a>. <code class="xref py py-meth docutils literal"><span class="pre">indices()</span></code> handles omitted and out-of-bounds indices in a
904
manner consistent with regular slices (and this innocuous phrase hides a welter
905
of confusing details!). The method is intended to be used like this:</p>
906
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">FakeSeq</span><span class="p">:</span>
907
<span class="o">...</span>
908
<span class="k">def</span> <span class="nf">calc_item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
909
<span class="o">...</span>
910
<span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
911
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="nb">slice</span><span class="p">):</span>
912
<span class="n">indices</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">indices</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
913
<span class="k">return</span> <span class="n">FakeSeq</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">calc_item</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="o">*</span><span class="n">indices</span><span class="p">)])</span>
914
<span class="k">else</span><span class="p">:</span>
915
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">calc_item</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
918
<p>From this example you can also see that the built-in <a class="reference internal" href="../library/functions.html#slice" title="slice"><code class="xref py py-class docutils literal"><span class="pre">slice</span></code></a> object is
919
now the type object for the slice type, and is no longer a function. This is
920
consistent with Python 2.2, where <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal"><span class="pre">int</span></code></a>, <a class="reference internal" href="../library/functions.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a>, etc., underwent
923
<div class="section" id="other-language-changes">
924
<h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Permalink to this headline">¶</a></h2>
925
<p>Here are all of the changes that Python 2.3 makes to the core Python language.</p>
927
<li><p class="first">The <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> statement is now always a keyword, as described in
928
section <a class="reference internal" href="#section-generators"><span>PEP 255: Simple Generators</span></a> of this document.</p>
930
<li><p class="first">A new built-in function <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><code class="xref py py-func docutils literal"><span class="pre">enumerate()</span></code></a> was added, as described in section
931
<a class="reference internal" href="#section-enumerate"><span>PEP 279: enumerate()</span></a> of this document.</p>
933
<li><p class="first">Two new constants, <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> and <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a> were added along with the
934
built-in <a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-class docutils literal"><span class="pre">bool</span></code></a> type, as described in section <a class="reference internal" href="#section-bool"><span>PEP 285: A Boolean Type</span></a> of this
937
<li><p class="first">The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal"><span class="pre">int()</span></code></a> type constructor will now return a long integer instead of
938
raising an <a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></code></a> when a string or floating-point number is too
939
large to fit into an integer. This can lead to the paradoxical result that
940
<code class="docutils literal"><span class="pre">isinstance(int(expression),</span> <span class="pre">int)</span></code> is false, but that seems unlikely to cause
941
problems in practice.</p>
943
<li><p class="first">Built-in types now support the extended slicing syntax, as described in
944
section <a class="reference internal" href="#section-slices"><span>Extended Slices</span></a> of this document.</p>
946
<li><p class="first">A new built-in function, <code class="xref py py-func docutils literal"><span class="pre">sum(iterable,</span> <span class="pre">start=0)()</span></code>, adds up the numeric
947
items in the iterable object and returns their sum. <a class="reference internal" href="../library/functions.html#sum" title="sum"><code class="xref py py-func docutils literal"><span class="pre">sum()</span></code></a> only accepts
948
numbers, meaning that you can’t use it to concatenate a bunch of strings.
949
(Contributed by Alex Martelli.)</p>
951
<li><p class="first"><code class="docutils literal"><span class="pre">list.insert(pos,</span> <span class="pre">value)</span></code> used to insert <em>value</em> at the front of the list
952
when <em>pos</em> was negative. The behaviour has now been changed to be consistent
953
with slice indexing, so when <em>pos</em> is -1 the value will be inserted before the
954
last element, and so forth.</p>
956
<li><p class="first"><code class="docutils literal"><span class="pre">list.index(value)</span></code>, which searches for <em>value</em> within the list and returns
957
its index, now takes optional <em>start</em> and <em>stop</em> arguments to limit the search
958
to only part of the list.</p>
960
<li><p class="first">Dictionaries have a new method, <code class="xref py py-meth docutils literal"><span class="pre">pop(key[,</span> <span class="pre">*default*])()</span></code>, that returns
961
the value corresponding to <em>key</em> and removes that key/value pair from the
962
dictionary. If the requested key isn’t present in the dictionary, <em>default</em> is
963
returned if it’s specified and <a class="reference internal" href="../library/exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></a> raised if it isn’t.</p>
964
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">d</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>
965
<span class="gp">>>> </span><span class="n">d</span>
966
<span class="go">{1: 2}</span>
967
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
968
<span class="gt">Traceback (most recent call last):</span>
969
File <span class="nb">"stdin"</span>, line <span class="m">1</span>, in <span class="n">?</span>
970
<span class="gr">KeyError</span>: <span class="n">4</span>
971
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
972
<span class="go">2</span>
973
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
974
<span class="gt">Traceback (most recent call last):</span>
975
File <span class="nb">"stdin"</span>, line <span class="m">1</span>, in <span class="n">?</span>
976
<span class="gr">KeyError</span>: <span class="n">'pop(): dictionary is empty'</span>
977
<span class="gp">>>> </span><span class="n">d</span>
978
<span class="go">{}</span>
979
<span class="go">>>></span>
982
<p>There’s also a new class method, <code class="xref py py-meth docutils literal"><span class="pre">dict.fromkeys(iterable,</span> <span class="pre">value)()</span></code>, that
983
creates a dictionary with keys taken from the supplied iterator <em>iterable</em> and
984
all values set to <em>value</em>, defaulting to <code class="docutils literal"><span class="pre">None</span></code>.</p>
985
<p>(Patches contributed by Raymond Hettinger.)</p>
986
<p>Also, the <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-func docutils literal"><span class="pre">dict()</span></code></a> constructor now accepts keyword arguments to simplify
987
creating small dictionaries:</p>
988
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">dict</span><span class="p">(</span><span class="n">red</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">blue</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">green</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">black</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
989
<span class="go">{'blue': 2, 'black': 4, 'green': 3, 'red': 1}</span>
992
<p>(Contributed by Just van Rossum.)</p>
994
<li><p class="first">The <a class="reference internal" href="../reference/simple_stmts.html#assert"><code class="xref std std-keyword docutils literal"><span class="pre">assert</span></code></a> statement no longer checks the <code class="docutils literal"><span class="pre">__debug__</span></code> flag, so
995
you can no longer disable assertions by assigning to <code class="docutils literal"><span class="pre">__debug__</span></code>. Running
996
Python with the <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal"><span class="pre">-O</span></code></a> switch will still generate code that doesn’t
997
execute any assertions.</p>
999
<li><p class="first">Most type objects are now callable, so you can use them to create new objects
1000
such as functions, classes, and modules. (This means that the <a class="reference internal" href="../library/new.html#module-new" title="new: Interface to the creation of runtime implementation objects. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">new</span></code></a> module
1001
can be deprecated in a future Python version, because you can now use the type
1002
objects available in the <a class="reference internal" href="../library/types.html#module-types" title="types: Names for built-in types."><code class="xref py py-mod docutils literal"><span class="pre">types</span></code></a> module.) For example, you can create a new
1003
module object with the following code:</p>
1004
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">types</span>
1005
<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">types</span><span class="o">.</span><span class="n">ModuleType</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">,</span><span class="s1">'docstring'</span><span class="p">)</span>
1006
<span class="gp">>>> </span><span class="n">m</span>
1007
<span class="go"><module 'abc' (built-in)></span>
1008
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">__doc__</span>
1009
<span class="go">'docstring'</span>
1013
<li><p class="first">A new warning, <a class="reference internal" href="../library/exceptions.html#exceptions.PendingDeprecationWarning" title="exceptions.PendingDeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></code></a> was added to indicate features
1014
which are in the process of being deprecated. The warning will <em>not</em> be printed
1015
by default. To check for use of features that will be deprecated in the future,
1016
supply <code class="xref std std-option docutils literal"><span class="pre">-Walways::PendingDeprecationWarning::</span></code> on the command line or
1017
use <a class="reference internal" href="../library/warnings.html#warnings.filterwarnings" title="warnings.filterwarnings"><code class="xref py py-func docutils literal"><span class="pre">warnings.filterwarnings()</span></code></a>.</p>
1019
<li><p class="first">The process of deprecating string-based exceptions, as in <code class="docutils literal"><span class="pre">raise</span> <span class="pre">"Error</span>
1020
<span class="pre">occurred"</span></code>, has begun. Raising a string will now trigger
1021
<a class="reference internal" href="../library/exceptions.html#exceptions.PendingDeprecationWarning" title="exceptions.PendingDeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></code></a>.</p>
1023
<li><p class="first">Using <code class="docutils literal"><span class="pre">None</span></code> as a variable name will now result in a <a class="reference internal" href="../library/exceptions.html#exceptions.SyntaxWarning" title="exceptions.SyntaxWarning"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxWarning</span></code></a>
1024
warning. In a future version of Python, <code class="docutils literal"><span class="pre">None</span></code> may finally become a keyword.</p>
1026
<li><p class="first">The <code class="xref py py-meth docutils literal"><span class="pre">xreadlines()</span></code> method of file objects, introduced in Python 2.1, is no
1027
longer necessary because files now behave as their own iterator.
1028
<code class="xref py py-meth docutils literal"><span class="pre">xreadlines()</span></code> was originally introduced as a faster way to loop over all
1029
the lines in a file, but now you can simply write <code class="docutils literal"><span class="pre">for</span> <span class="pre">line</span> <span class="pre">in</span> <span class="pre">file_obj</span></code>.
1030
File objects also have a new read-only <code class="xref py py-attr docutils literal"><span class="pre">encoding</span></code> attribute that gives the
1031
encoding used by the file; Unicode strings written to the file will be
1032
automatically converted to bytes using the given encoding.</p>
1034
<li><p class="first">The method resolution order used by new-style classes has changed, though
1035
you’ll only notice the difference if you have a really complicated inheritance
1036
hierarchy. Classic classes are unaffected by this change. Python 2.2
1037
originally used a topological sort of a class’s ancestors, but 2.3 now uses the
1038
C3 algorithm as described in the paper <a class="reference external" href="http://www.webcom.com/haahr/dylan/linearization-oopsla96.html">“A Monotonic Superclass Linearization
1039
for Dylan”</a>. To
1040
understand the motivation for this change, read Michele Simionato’s article
1041
<a class="reference external" href="https://www.python.org/2.3/mro.html">“Python 2.3 Method Resolution Order”</a>, or
1042
read the thread on python-dev starting with the message at
1043
<a class="reference external" href="https://mail.python.org/pipermail/python-dev/2002-October/029035.html">https://mail.python.org/pipermail/python-dev/2002-October/029035.html</a>. Samuele
1044
Pedroni first pointed out the problem and also implemented the fix by coding the
1047
<li><p class="first">Python runs multithreaded programs by switching between threads after
1048
executing N bytecodes. The default value for N has been increased from 10 to
1049
100 bytecodes, speeding up single-threaded applications by reducing the
1050
switching overhead. Some multithreaded applications may suffer slower response
1051
time, but that’s easily fixed by setting the limit back to a lower number using
1052
<code class="xref py py-func docutils literal"><span class="pre">sys.setcheckinterval(N)()</span></code>. The limit can be retrieved with the new
1053
<a class="reference internal" href="../library/sys.html#sys.getcheckinterval" title="sys.getcheckinterval"><code class="xref py py-func docutils literal"><span class="pre">sys.getcheckinterval()</span></code></a> function.</p>
1055
<li><p class="first">One minor but far-reaching change is that the names of extension types defined
1056
by the modules included with Python now contain the module and a <code class="docutils literal"><span class="pre">'.'</span></code> in
1057
front of the type name. For example, in Python 2.2, if you created a socket and
1058
printed its <code class="xref py py-attr docutils literal"><span class="pre">__class__</span></code>, you’d get this output:</p>
1059
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">()</span>
1060
<span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">__class__</span>
1061
<span class="go"><type 'socket'></span>
1064
<p>In 2.3, you get this:</p>
1065
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">__class__</span>
1066
<span class="go"><type '_socket.socket'></span>
1070
<li><p class="first">One of the noted incompatibilities between old- and new-style classes has been
1071
removed: you can now assign to the <code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code> and <code class="xref py py-attr docutils literal"><span class="pre">__bases__</span></code>
1072
attributes of new-style classes. There are some restrictions on what can be
1073
assigned to <code class="xref py py-attr docutils literal"><span class="pre">__bases__</span></code> along the lines of those relating to assigning to
1074
an instance’s <code class="xref py py-attr docutils literal"><span class="pre">__class__</span></code> attribute.</p>
1077
<div class="section" id="string-changes">
1078
<h3>String Changes<a class="headerlink" href="#string-changes" title="Permalink to this headline">¶</a></h3>
1080
<li><p class="first">The <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal"><span class="pre">in</span></code></a> operator now works differently for strings. Previously, when
1081
evaluating <code class="docutils literal"><span class="pre">X</span> <span class="pre">in</span> <span class="pre">Y</span></code> where <em>X</em> and <em>Y</em> are strings, <em>X</em> could only be a single
1082
character. That’s now changed; <em>X</em> can be a string of any length, and <code class="docutils literal"><span class="pre">X</span> <span class="pre">in</span> <span class="pre">Y</span></code>
1083
will return <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> if <em>X</em> is a substring of <em>Y</em>. If <em>X</em> is the empty
1084
string, the result is always <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>.</p>
1085
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s1">'ab'</span> <span class="ow">in</span> <span class="s1">'abcd'</span>
1086
<span class="go">True</span>
1087
<span class="gp">>>> </span><span class="s1">'ad'</span> <span class="ow">in</span> <span class="s1">'abcd'</span>
1088
<span class="go">False</span>
1089
<span class="gp">>>> </span><span class="s1">''</span> <span class="ow">in</span> <span class="s1">'abcd'</span>
1090
<span class="go">True</span>
1093
<p>Note that this doesn’t tell you where the substring starts; if you need that
1094
information, use the <code class="xref py py-meth docutils literal"><span class="pre">find()</span></code> string method.</p>
1096
<li><p class="first">The <code class="xref py py-meth docutils literal"><span class="pre">strip()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">lstrip()</span></code>, and <code class="xref py py-meth docutils literal"><span class="pre">rstrip()</span></code> string methods now have
1097
an optional argument for specifying the characters to strip. The default is
1098
still to remove all whitespace characters:</p>
1099
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s1">' abc '</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
1100
<span class="go">'abc'</span>
1101
<span class="gp">>>> </span><span class="s1">'><><abc<><><>'</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">'<>'</span><span class="p">)</span>
1102
<span class="go">'abc'</span>
1103
<span class="gp">>>> </span><span class="s1">'><><abc<><><></span><span class="se">\n</span><span class="s1">'</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">'<>'</span><span class="p">)</span>
1104
<span class="go">'abc<><><>\n'</span>
1105
<span class="gp">>>> </span><span class="s1">u'</span><span class="se">\u4000\u4001</span><span class="s1">abc</span><span class="se">\u4000</span><span class="s1">'</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">u'</span><span class="se">\u4000</span><span class="s1">'</span><span class="p">)</span>
1106
<span class="go">u'\u4001abc'</span>
1107
<span class="go">>>></span>
1110
<p>(Suggested by Simon Brunning and implemented by Walter Dörwald.)</p>
1112
<li><p class="first">The <code class="xref py py-meth docutils literal"><span class="pre">startswith()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">endswith()</span></code> string methods now accept negative
1113
numbers for the <em>start</em> and <em>end</em> parameters.</p>
1115
<li><p class="first">Another new string method is <code class="xref py py-meth docutils literal"><span class="pre">zfill()</span></code>, originally a function in the
1116
<a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal"><span class="pre">string</span></code></a> module. <code class="xref py py-meth docutils literal"><span class="pre">zfill()</span></code> pads a numeric string with zeros on the
1117
left until it’s the specified width. Note that the <code class="docutils literal"><span class="pre">%</span></code> operator is still more
1118
flexible and powerful than <code class="xref py py-meth docutils literal"><span class="pre">zfill()</span></code>.</p>
1119
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s1">'45'</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
1120
<span class="go">'0045'</span>
1121
<span class="gp">>>> </span><span class="s1">'12345'</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
1122
<span class="go">'12345'</span>
1123
<span class="gp">>>> </span><span class="s1">'goofy'</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
1124
<span class="go">'0goofy'</span>
1127
<p>(Contributed by Walter Dörwald.)</p>
1129
<li><p class="first">A new type object, <a class="reference internal" href="../library/functions.html#basestring" title="basestring"><code class="xref py py-class docutils literal"><span class="pre">basestring</span></code></a>, has been added. Both 8-bit strings and
1130
Unicode strings inherit from this type, so <code class="docutils literal"><span class="pre">isinstance(obj,</span> <span class="pre">basestring)</span></code> will
1131
return <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> for either kind of string. It’s a completely abstract
1132
type, so you can’t create <a class="reference internal" href="../library/functions.html#basestring" title="basestring"><code class="xref py py-class docutils literal"><span class="pre">basestring</span></code></a> instances.</p>
1134
<li><p class="first">Interned strings are no longer immortal and will now be garbage-collected in
1135
the usual way when the only reference to them is from the internal dictionary of
1136
interned strings. (Implemented by Oren Tirosh.)</p>
1140
<div class="section" id="optimizations">
1141
<h3>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h3>
1143
<li>The creation of new-style class instances has been made much faster; they’re
1144
now faster than classic classes!</li>
1145
<li>The <code class="xref py py-meth docutils literal"><span class="pre">sort()</span></code> method of list objects has been extensively rewritten by Tim
1146
Peters, and the implementation is significantly faster.</li>
1147
<li>Multiplication of large long integers is now much faster thanks to an
1148
implementation of Karatsuba multiplication, an algorithm that scales better than
1149
the O(n*n) required for the grade-school multiplication algorithm. (Original
1150
patch by Christopher A. Craig, and significantly reworked by Tim Peters.)</li>
1151
<li>The <code class="docutils literal"><span class="pre">SET_LINENO</span></code> opcode is now gone. This may provide a small speed
1152
increase, depending on your compiler’s idiosyncrasies. See section
1153
<a class="reference internal" href="#section-other"><span>Other Changes and Fixes</span></a> for a longer explanation. (Removed by Michael Hudson.)</li>
1154
<li><a class="reference internal" href="../library/functions.html#xrange" title="xrange"><code class="xref py py-func docutils literal"><span class="pre">xrange()</span></code></a> objects now have their own iterator, making <code class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span>
1155
<span class="pre">xrange(n)</span></code> slightly faster than <code class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">range(n)</span></code>. (Patch by Raymond
1157
<li>A number of small rearrangements have been made in various hotspots to improve
1158
performance, such as inlining a function or removing some code. (Implemented
1159
mostly by GvR, but lots of people have contributed single changes.)</li>
1161
<p>The net result of the 2.3 optimizations is that Python 2.3 runs the pystone
1162
benchmark around 25% faster than Python 2.2.</p>
1165
<div class="section" id="new-improved-and-deprecated-modules">
1166
<h2>New, Improved, and Deprecated Modules<a class="headerlink" href="#new-improved-and-deprecated-modules" title="Permalink to this headline">¶</a></h2>
1167
<p>As usual, Python’s standard library received a number of enhancements and bug
1168
fixes. Here’s a partial list of the most notable changes, sorted alphabetically
1169
by module name. Consult the <code class="file docutils literal"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more
1170
complete list of changes, or look through the CVS logs for all the details.</p>
1172
<li><p class="first">The <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal"><span class="pre">array</span></code></a> module now supports arrays of Unicode characters using the
1173
<code class="docutils literal"><span class="pre">'u'</span></code> format character. Arrays also now support using the <code class="docutils literal"><span class="pre">+=</span></code> assignment
1174
operator to add another array’s contents, and the <code class="docutils literal"><span class="pre">*=</span></code> assignment operator to
1175
repeat an array. (Contributed by Jason Orendorff.)</p>
1177
<li><p class="first">The <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><code class="xref py py-mod docutils literal"><span class="pre">bsddb</span></code></a> module has been replaced by version 4.1.6 of the <a class="reference external" href="http://pybsddb.sourceforge.net">PyBSDDB</a> package, providing a more complete interface
1178
to the transactional features of the BerkeleyDB library.</p>
1179
<p>The old version of the module has been renamed to <code class="xref py py-mod docutils literal"><span class="pre">bsddb185</span></code> and is no
1180
longer built automatically; you’ll have to edit <code class="file docutils literal"><span class="pre">Modules/Setup</span></code> to enable
1181
it. Note that the new <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><code class="xref py py-mod docutils literal"><span class="pre">bsddb</span></code></a> package is intended to be compatible with
1182
the old module, so be sure to file bugs if you discover any incompatibilities.
1183
When upgrading to Python 2.3, if the new interpreter is compiled with a new
1184
version of the underlying BerkeleyDB library, you will almost certainly have to
1185
convert your database files to the new version. You can do this fairly easily
1186
with the new scripts <code class="file docutils literal"><span class="pre">db2pickle.py</span></code> and <code class="file docutils literal"><span class="pre">pickle2db.py</span></code> which you
1187
will find in the distribution’s <code class="file docutils literal"><span class="pre">Tools/scripts</span></code> directory. If you’ve
1188
already been using the PyBSDDB package and importing it as <code class="xref py py-mod docutils literal"><span class="pre">bsddb3</span></code>, you
1189
will have to change your <code class="docutils literal"><span class="pre">import</span></code> statements to import it as <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><code class="xref py py-mod docutils literal"><span class="pre">bsddb</span></code></a>.</p>
1191
<li><p class="first">The new <a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interface to compression and decompression routines compatible with bzip2."><code class="xref py py-mod docutils literal"><span class="pre">bz2</span></code></a> module is an interface to the bz2 data compression library.
1192
bz2-compressed data is usually smaller than corresponding <a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal"><span class="pre">zlib</span></code></a>-compressed data. (Contributed by Gustavo Niemeyer.)</p>
1194
<li><p class="first">A set of standard date/time types has been added in the new <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal"><span class="pre">datetime</span></code></a>
1195
module. See the following section for more details.</p>
1197
<li><p class="first">The Distutils <code class="xref py py-class docutils literal"><span class="pre">Extension</span></code> class now supports an extra constructor
1198
argument named <em>depends</em> for listing additional source files that an extension
1199
depends on. This lets Distutils recompile the module if any of the dependency
1200
files are modified. For example, if <code class="file docutils literal"><span class="pre">sampmodule.c</span></code> includes the header
1201
file <code class="file docutils literal"><span class="pre">sample.h</span></code>, you would create the <code class="xref py py-class docutils literal"><span class="pre">Extension</span></code> object like
1203
<div class="highlight-python"><div class="highlight"><pre><span class="n">ext</span> <span class="o">=</span> <span class="n">Extension</span><span class="p">(</span><span class="s2">"samp"</span><span class="p">,</span>
1204
<span class="n">sources</span><span class="o">=</span><span class="p">[</span><span class="s2">"sampmodule.c"</span><span class="p">],</span>
1205
<span class="n">depends</span><span class="o">=</span><span class="p">[</span><span class="s2">"sample.h"</span><span class="p">])</span>
1208
<p>Modifying <code class="file docutils literal"><span class="pre">sample.h</span></code> would then cause the module to be recompiled.
1209
(Contributed by Jeremy Hylton.)</p>
1211
<li><p class="first">Other minor changes to Distutils: it now checks for the <span class="target" id="index-24"></span><code class="xref std std-envvar docutils literal"><span class="pre">CC</span></code>,
1212
<span class="target" id="index-25"></span><code class="xref std std-envvar docutils literal"><span class="pre">CFLAGS</span></code>, <span class="target" id="index-26"></span><code class="xref std std-envvar docutils literal"><span class="pre">CPP</span></code>, <span class="target" id="index-27"></span><code class="xref std std-envvar docutils literal"><span class="pre">LDFLAGS</span></code>, and <span class="target" id="index-28"></span><code class="xref std std-envvar docutils literal"><span class="pre">CPPFLAGS</span></code>
1213
environment variables, using them to override the settings in Python’s
1214
configuration (contributed by Robert Weber).</p>
1216
<li><p class="first">Previously the <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal"><span class="pre">doctest</span></code></a> module would only search the docstrings of
1217
public methods and functions for test cases, but it now also examines private
1218
ones as well. The <code class="xref py py-func docutils literal"><span class="pre">DocTestSuite(()</span></code> function creates a
1219
<a class="reference internal" href="../library/unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal"><span class="pre">unittest.TestSuite</span></code></a> object from a set of <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal"><span class="pre">doctest</span></code></a> tests.</p>
1221
<li><p class="first">The new <code class="xref py py-func docutils literal"><span class="pre">gc.get_referents(object)()</span></code> function returns a list of all the
1222
objects referenced by <em>object</em>.</p>
1224
<li><p class="first">The <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal"><span class="pre">getopt</span></code></a> module gained a new function, <code class="xref py py-func docutils literal"><span class="pre">gnu_getopt()</span></code>, that
1225
supports the same arguments as the existing <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-func docutils literal"><span class="pre">getopt()</span></code></a> function but uses
1226
GNU-style scanning mode. The existing <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-func docutils literal"><span class="pre">getopt()</span></code></a> stops processing options as
1227
soon as a non-option argument is encountered, but in GNU-style mode processing
1228
continues, meaning that options and arguments can be mixed. For example:</p>
1229
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">getopt</span><span class="o">.</span><span class="n">getopt</span><span class="p">([</span><span class="s1">'-f'</span><span class="p">,</span> <span class="s1">'filename'</span><span class="p">,</span> <span class="s1">'output'</span><span class="p">,</span> <span class="s1">'-v'</span><span class="p">],</span> <span class="s1">'f:v'</span><span class="p">)</span>
1230
<span class="go">([('-f', 'filename')], ['output', '-v'])</span>
1231
<span class="gp">>>> </span><span class="n">getopt</span><span class="o">.</span><span class="n">gnu_getopt</span><span class="p">([</span><span class="s1">'-f'</span><span class="p">,</span> <span class="s1">'filename'</span><span class="p">,</span> <span class="s1">'output'</span><span class="p">,</span> <span class="s1">'-v'</span><span class="p">],</span> <span class="s1">'f:v'</span><span class="p">)</span>
1232
<span class="go">([('-f', 'filename'), ('-v', '')], ['output'])</span>
1235
<p>(Contributed by Peter Åstrand.)</p>
1237
<li><p class="first">The <a class="reference internal" href="../library/grp.html#module-grp" title="grp: The group database (getgrnam() and friends). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">grp</span></code></a>, <a class="reference internal" href="../library/pwd.html#module-pwd" title="pwd: The password database (getpwnam() and friends). (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">pwd</span></code></a>, and <a class="reference internal" href="../library/resource.html#module-resource" title="resource: An interface to provide resource usage information on the current process. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">resource</span></code></a> modules now return enhanced
1239
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">grp</span>
1240
<span class="gp">>>> </span><span class="n">g</span> <span class="o">=</span> <span class="n">grp</span><span class="o">.</span><span class="n">getgrnam</span><span class="p">(</span><span class="s1">'amk'</span><span class="p">)</span>
1241
<span class="gp">>>> </span><span class="n">g</span><span class="o">.</span><span class="n">gr_name</span><span class="p">,</span> <span class="n">g</span><span class="o">.</span><span class="n">gr_gid</span>
1242
<span class="go">('amk', 500)</span>
1246
<li><p class="first">The <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"><span class="pre">gzip</span></code></a> module can now handle files exceeding 2 GiB.</p>
1248
<li><p class="first">The new <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal"><span class="pre">heapq</span></code></a> module contains an implementation of a heap queue
1249
algorithm. A heap is an array-like data structure that keeps items in a
1250
partially sorted order such that, for every index <em>k</em>, <code class="docutils literal"><span class="pre">heap[k]</span> <span class="pre"><=</span>
1251
<span class="pre">heap[2*k+1]</span></code> and <code class="docutils literal"><span class="pre">heap[k]</span> <span class="pre"><=</span> <span class="pre">heap[2*k+2]</span></code>. This makes it quick to remove the
1252
smallest item, and inserting a new item while maintaining the heap property is
1253
O(lg n). (See <a class="reference external" href="http://www.nist.gov/dads/HTML/priorityque.html">http://www.nist.gov/dads/HTML/priorityque.html</a> for more
1254
information about the priority queue data structure.)</p>
1255
<p>The <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal"><span class="pre">heapq</span></code></a> module provides <code class="xref py py-func docutils literal"><span class="pre">heappush()</span></code> and <code class="xref py py-func docutils literal"><span class="pre">heappop()</span></code> functions
1256
for adding and removing items while maintaining the heap property on top of some
1257
other mutable Python sequence type. Here’s an example that uses a Python list:</p>
1258
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">heapq</span>
1259
<span class="gp">>>> </span><span class="n">heap</span> <span class="o">=</span> <span class="p">[]</span>
1260
<span class="gp">>>> </span><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">]:</span>
1261
<span class="gp">... </span> <span class="n">heapq</span><span class="o">.</span><span class="n">heappush</span><span class="p">(</span><span class="n">heap</span><span class="p">,</span> <span class="n">item</span><span class="p">)</span>
1262
<span class="gp">...</span>
1263
<span class="gp">>>> </span><span class="n">heap</span>
1264
<span class="go">[1, 3, 5, 11, 7]</span>
1265
<span class="gp">>>> </span><span class="n">heapq</span><span class="o">.</span><span class="n">heappop</span><span class="p">(</span><span class="n">heap</span><span class="p">)</span>
1266
<span class="go">1</span>
1267
<span class="gp">>>> </span><span class="n">heapq</span><span class="o">.</span><span class="n">heappop</span><span class="p">(</span><span class="n">heap</span><span class="p">)</span>
1268
<span class="go">3</span>
1269
<span class="gp">>>> </span><span class="n">heap</span>
1270
<span class="go">[5, 7, 11]</span>
1273
<p>(Contributed by Kevin O’Connor.)</p>
1275
<li><p class="first">The IDLE integrated development environment has been updated using the code
1276
from the IDLEfork project (<a class="reference external" href="http://idlefork.sourceforge.net">http://idlefork.sourceforge.net</a>). The most notable feature is
1277
that the code being developed is now executed in a subprocess, meaning that
1278
there’s no longer any need for manual <code class="docutils literal"><span class="pre">reload()</span></code> operations. IDLE’s core code
1279
has been incorporated into the standard library as the <code class="xref py py-mod docutils literal"><span class="pre">idlelib</span></code> package.</p>
1281
<li><p class="first">The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">imaplib</span></code></a> module now supports IMAP over SSL. (Contributed by Piers
1282
Lauder and Tino Lange.)</p>
1284
<li><p class="first">The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal"><span class="pre">itertools</span></code></a> contains a number of useful functions for use with
1285
iterators, inspired by various functions provided by the ML and Haskell
1286
languages. For example, <code class="docutils literal"><span class="pre">itertools.ifilter(predicate,</span> <span class="pre">iterator)</span></code> returns all
1287
elements in the iterator for which the function <code class="xref py py-func docutils literal"><span class="pre">predicate()</span></code> returns
1288
<a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a>, and <code class="docutils literal"><span class="pre">itertools.repeat(obj,</span> <span class="pre">N)</span></code> returns <code class="docutils literal"><span class="pre">obj</span></code> <em>N</em> times.
1289
There are a number of other functions in the module; see the package’s reference
1290
documentation for details.
1291
(Contributed by Raymond Hettinger.)</p>
1293
<li><p class="first">Two new functions in the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal"><span class="pre">math</span></code></a> module, <code class="xref py py-func docutils literal"><span class="pre">degrees(rads)()</span></code> and
1294
<code class="xref py py-func docutils literal"><span class="pre">radians(degs)()</span></code>, convert between radians and degrees. Other functions in
1295
the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal"><span class="pre">math</span></code></a> module such as <a class="reference internal" href="../library/math.html#math.sin" title="math.sin"><code class="xref py py-func docutils literal"><span class="pre">math.sin()</span></code></a> and <a class="reference internal" href="../library/math.html#math.cos" title="math.cos"><code class="xref py py-func docutils literal"><span class="pre">math.cos()</span></code></a> have always
1296
required input values measured in radians. Also, an optional <em>base</em> argument
1297
was added to <a class="reference internal" href="../library/math.html#math.log" title="math.log"><code class="xref py py-func docutils literal"><span class="pre">math.log()</span></code></a> to make it easier to compute logarithms for bases
1298
other than <code class="docutils literal"><span class="pre">e</span></code> and <code class="docutils literal"><span class="pre">10</span></code>. (Contributed by Raymond Hettinger.)</p>
1300
<li><p class="first">Several new POSIX functions (<code class="xref py py-func docutils literal"><span class="pre">getpgid()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">killpg()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">lchown()</span></code>,
1301
<code class="xref py py-func docutils literal"><span class="pre">loadavg()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">major()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">makedev()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">minor()</span></code>, and
1302
<code class="xref py py-func docutils literal"><span class="pre">mknod()</span></code>) were added to the <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"><span class="pre">posix</span></code></a> module that underlies the
1303
<a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a> module. (Contributed by Gustavo Niemeyer, Geert Jansen, and Denis S.
1306
<li><p class="first">In the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a> module, the <code class="xref py py-func docutils literal"><span class="pre">*stat()</span></code> family of functions can now report
1307
fractions of a second in a timestamp. Such time stamps are represented as
1308
floats, similar to the value returned by <a class="reference internal" href="../library/time.html#time.time" title="time.time"><code class="xref py py-func docutils literal"><span class="pre">time.time()</span></code></a>.</p>
1309
<p>During testing, it was found that some applications will break if time stamps
1310
are floats. For compatibility, when using the tuple interface of the
1311
<code class="xref py py-class docutils literal"><span class="pre">stat_result</span></code> time stamps will be represented as integers. When using
1312
named fields (a feature first introduced in Python 2.2), time stamps are still
1313
represented as integers, unless <a class="reference internal" href="../library/os.html#os.stat_float_times" title="os.stat_float_times"><code class="xref py py-func docutils literal"><span class="pre">os.stat_float_times()</span></code></a> is invoked to enable
1314
float return values:</p>
1315
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s2">"/tmp"</span><span class="p">)</span><span class="o">.</span><span class="n">st_mtime</span>
1316
<span class="go">1034791200</span>
1317
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">stat_float_times</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
1318
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="s2">"/tmp"</span><span class="p">)</span><span class="o">.</span><span class="n">st_mtime</span>
1319
<span class="go">1034791200.6335014</span>
1322
<p>In Python 2.4, the default will change to always returning floats.</p>
1323
<p>Application developers should enable this feature only if all their libraries
1324
work properly when confronted with floating point time stamps, or if they use
1325
the tuple API. If used, the feature should be activated on an application level
1326
instead of trying to enable it on a per-use basis.</p>
1328
<li><p class="first">The <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a> module contains a new parser for command-line arguments
1329
that can convert option values to a particular Python type and will
1330
automatically generate a usage message. See the following section for more
1333
<li><p class="first">The old and never-documented <code class="xref py py-mod docutils literal"><span class="pre">linuxaudiodev</span></code> module has been deprecated,
1334
and a new version named <a class="reference internal" href="../library/ossaudiodev.html#module-ossaudiodev" title="ossaudiodev: Access to OSS-compatible audio devices. (Linux, FreeBSD)"><code class="xref py py-mod docutils literal"><span class="pre">ossaudiodev</span></code></a> has been added. The module was
1335
renamed because the OSS sound drivers can be used on platforms other than Linux,
1336
and the interface has also been tidied and brought up to date in various ways.
1337
(Contributed by Greg Ward and Nicholas FitzRoy-Dale.)</p>
1339
<li><p class="first">The new <a class="reference internal" href="../library/platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code class="xref py py-mod docutils literal"><span class="pre">platform</span></code></a> module contains a number of functions that try to
1340
determine various properties of the platform you’re running on. There are
1341
functions for getting the architecture, CPU type, the Windows OS version, and
1342
even the Linux distribution version. (Contributed by Marc-André Lemburg.)</p>
1344
<li><p class="first">The parser objects provided by the <code class="xref py py-mod docutils literal"><span class="pre">pyexpat</span></code> module can now optionally
1345
buffer character data, resulting in fewer calls to your character data handler
1346
and therefore faster performance. Setting the parser object’s
1347
<code class="xref py py-attr docutils literal"><span class="pre">buffer_text</span></code> attribute to <a class="reference internal" href="../library/constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> will enable buffering.</p>
1349
<li><p class="first">The <code class="xref py py-func docutils literal"><span class="pre">sample(population,</span> <span class="pre">k)()</span></code> function was added to the <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal"><span class="pre">random</span></code></a>
1350
module. <em>population</em> is a sequence or <a class="reference internal" href="../library/functions.html#xrange" title="xrange"><code class="xref py py-class docutils literal"><span class="pre">xrange</span></code></a> object containing the
1351
elements of a population, and <code class="xref py py-func docutils literal"><span class="pre">sample()</span></code> chooses <em>k</em> elements from the
1352
population without replacing chosen elements. <em>k</em> can be any value up to
1353
<code class="docutils literal"><span class="pre">len(population)</span></code>. For example:</p>
1354
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">days</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Mo'</span><span class="p">,</span> <span class="s1">'Tu'</span><span class="p">,</span> <span class="s1">'We'</span><span class="p">,</span> <span class="s1">'Th'</span><span class="p">,</span> <span class="s1">'Fr'</span><span class="p">,</span> <span class="s1">'St'</span><span class="p">,</span> <span class="s1">'Sn'</span><span class="p">]</span>
1355
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">days</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># Choose 3 elements</span>
1356
<span class="go">['St', 'Sn', 'Th']</span>
1357
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">days</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span> <span class="c1"># Choose 7 elements</span>
1358
<span class="go">['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']</span>
1359
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">days</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span> <span class="c1"># Choose 7 again</span>
1360
<span class="go">['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']</span>
1361
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">days</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span> <span class="c1"># Can't choose eight</span>
1362
<span class="gt">Traceback (most recent call last):</span>
1363
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span>
1364
File <span class="nb">"random.py"</span>, line <span class="m">414</span>, in <span class="n">sample</span>
1365
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">,</span> <span class="s2">"sample larger than population"</span>
1366
<span class="gr">ValueError</span>: <span class="n">sample larger than population</span>
1367
<span class="gp">>>> </span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">10000</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span> <span class="mi">10</span><span class="p">)</span> <span class="c1"># Choose ten odd nos. under 10000</span>
1368
<span class="go">[3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195]</span>
1371
<p>The <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal"><span class="pre">random</span></code></a> module now uses a new algorithm, the Mersenne Twister,
1372
implemented in C. It’s faster and more extensively studied than the previous
1374
<p>(All changes contributed by Raymond Hettinger.)</p>
1376
<li><p class="first">The <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"><span class="pre">readline</span></code></a> module also gained a number of new functions:
1377
<code class="xref py py-func docutils literal"><span class="pre">get_history_item()</span></code>, <code class="xref py py-func docutils literal"><span class="pre">get_current_history_length()</span></code>, and
1378
<code class="xref py py-func docutils literal"><span class="pre">redisplay()</span></code>.</p>
1380
<li><p class="first">The <a class="reference internal" href="../library/rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a> and <a class="reference internal" href="../library/bastion.html#module-Bastion" title="Bastion: Providing restricted access to objects. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">Bastion</span></code></a> modules have been declared dead, and
1381
attempts to import them will fail with a <a class="reference internal" href="../library/exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><code class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></code></a>. New-style classes
1382
provide new ways to break out of the restricted execution environment provided
1383
by <a class="reference internal" href="../library/rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a>, and no one has interest in fixing them or time to do so. If
1384
you have applications using <a class="reference internal" href="../library/rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a>, rewrite them to use something else.</p>
1385
<p>(Sticking with Python 2.2 or 2.1 will not make your applications any safer
1386
because there are known bugs in the <a class="reference internal" href="../library/rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a> module in those versions. To
1387
repeat: if you’re using <a class="reference internal" href="../library/rexec.html#module-rexec" title="rexec: Basic restricted execution framework. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">rexec</span></code></a>, stop using it immediately.)</p>
1389
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">rotor</span></code> module has been deprecated because the algorithm it uses for
1390
encryption is not believed to be secure. If you need encryption, use one of the
1391
several AES Python modules that are available separately.</p>
1393
<li><p class="first">The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a> module gained a <code class="xref py py-func docutils literal"><span class="pre">move(src,</span> <span class="pre">dest)()</span></code> function that
1394
recursively moves a file or directory to a new location.</p>
1396
<li><p class="first">Support for more advanced POSIX signal handling was added to the <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal"><span class="pre">signal</span></code></a>
1397
but then removed again as it proved impossible to make it work reliably across
1400
<li><p class="first">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"><span class="pre">socket</span></code></a> module now supports timeouts. You can call the
1401
<code class="xref py py-meth docutils literal"><span class="pre">settimeout(t)()</span></code> method on a socket object to set a timeout of <em>t</em> seconds.
1402
Subsequent socket operations that take longer than <em>t</em> seconds to complete will
1403
abort and raise a <a class="reference internal" href="../library/socket.html#socket.timeout" title="socket.timeout"><code class="xref py py-exc docutils literal"><span class="pre">socket.timeout</span></code></a> exception.</p>
1404
<p>The original timeout implementation was by Tim O’Malley. Michael Gilfix
1405
integrated it into the Python <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a> module and shepherded it through a
1406
lengthy review. After the code was checked in, Guido van Rossum rewrote parts
1407
of it. (This is a good example of a collaborative development process in
1410
<li><p class="first">On Windows, 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"><span class="pre">socket</span></code></a> module now ships with Secure Sockets Layer
1413
<li><p class="first">The value of the C <code class="xref py py-const docutils literal"><span class="pre">PYTHON_API_VERSION</span></code> macro is now exposed at the
1414
Python level as <code class="docutils literal"><span class="pre">sys.api_version</span></code>. The current exception can be cleared by
1415
calling the new <a class="reference internal" href="../library/sys.html#sys.exc_clear" title="sys.exc_clear"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_clear()</span></code></a> function.</p>
1417
<li><p class="first">The new <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> module allows reading from and writing to
1418
<strong class="program">tar</strong>-format archive files. (Contributed by Lars Gustäbel.)</p>
1420
<li><p class="first">The new <a class="reference internal" href="../library/textwrap.html#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal"><span class="pre">textwrap</span></code></a> module contains functions for wrapping strings
1421
containing paragraphs of text. The <code class="xref py py-func docutils literal"><span class="pre">wrap(text,</span> <span class="pre">width)()</span></code> function takes a
1422
string and returns a list containing the text split into lines of no more than
1423
the chosen width. The <code class="xref py py-func docutils literal"><span class="pre">fill(text,</span> <span class="pre">width)()</span></code> function returns a single
1424
string, reformatted to fit into lines no longer than the chosen width. (As you
1425
can guess, <code class="xref py py-func docutils literal"><span class="pre">fill()</span></code> is built on top of <code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code>. For example:</p>
1426
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">textwrap</span>
1427
<span class="gp">>>> </span><span class="n">paragraph</span> <span class="o">=</span> <span class="s2">"Not a whit, we defy augury: ... more text ..."</span>
1428
<span class="gp">>>> </span><span class="n">textwrap</span><span class="o">.</span><span class="n">wrap</span><span class="p">(</span><span class="n">paragraph</span><span class="p">,</span> <span class="mi">60</span><span class="p">)</span>
1429
<span class="go">["Not a whit, we defy augury: there's a special providence in",</span>
1430
<span class="go"> "the fall of a sparrow. If it be now, 'tis not to come; if it",</span>
1431
<span class="go"> ...]</span>
1432
<span class="gp">>>> </span><span class="k">print</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">paragraph</span><span class="p">,</span> <span class="mi">35</span><span class="p">)</span>
1433
<span class="go">Not a whit, we defy augury: there's</span>
1434
<span class="go">a special providence in the fall of</span>
1435
<span class="go">a sparrow. If it be now, 'tis not</span>
1436
<span class="go">to come; if it be not to come, it</span>
1437
<span class="go">will be now; if it be not now, yet</span>
1438
<span class="go">it will come: the readiness is all.</span>
1439
<span class="go">>>></span>
1442
<p>The module also contains a <code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code> class that actually implements
1443
the text wrapping strategy. Both the <code class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></code> class and the
1444
<code class="xref py py-func docutils literal"><span class="pre">wrap()</span></code> and <code class="xref py py-func docutils literal"><span class="pre">fill()</span></code> functions support a number of additional keyword
1445
arguments for fine-tuning the formatting; consult the module’s documentation
1446
for details. (Contributed by Greg Ward.)</p>
1448
<li><p class="first">The <a class="reference internal" href="../library/thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><code class="xref py py-mod docutils literal"><span class="pre">thread</span></code></a> and <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><code class="xref py py-mod docutils literal"><span class="pre">threading</span></code></a> modules now have companion modules,
1449
<a class="reference internal" href="../library/dummy_thread.html#module-dummy_thread" title="dummy_thread: Drop-in replacement for the thread module."><code class="xref py py-mod docutils literal"><span class="pre">dummy_thread</span></code></a> and <a class="reference internal" href="../library/dummy_threading.html#module-dummy_threading" title="dummy_threading: Drop-in replacement for the threading module."><code class="xref py py-mod docutils literal"><span class="pre">dummy_threading</span></code></a>, that provide a do-nothing
1450
implementation of the <a class="reference internal" href="../library/thread.html#module-thread" title="thread: Create multiple threads of control within one interpreter."><code class="xref py py-mod docutils literal"><span class="pre">thread</span></code></a> module’s interface for platforms where
1451
threads are not supported. The intention is to simplify thread-aware modules
1452
(ones that <em>don’t</em> rely on threads to run) by putting the following code at the
1454
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
1455
<span class="kn">import</span> <span class="nn">threading</span> <span class="kn">as</span> <span class="nn">_threading</span>
1456
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
1457
<span class="kn">import</span> <span class="nn">dummy_threading</span> <span class="kn">as</span> <span class="nn">_threading</span>
1460
<p>In this example, <code class="xref py py-mod docutils literal"><span class="pre">_threading</span></code> is used as the module name to make it clear
1461
that the module being used is not necessarily the actual <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><code class="xref py py-mod docutils literal"><span class="pre">threading</span></code></a>
1462
module. Code can call functions and use classes in <code class="xref py py-mod docutils literal"><span class="pre">_threading</span></code> whether or
1463
not threads are supported, avoiding an <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal"><span class="pre">if</span></code></a> statement and making the
1464
code slightly clearer. This module will not magically make multithreaded code
1465
run without threads; code that waits for another thread to return or to do
1466
something will simply hang forever.</p>
1468
<li><p class="first">The <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal"><span class="pre">time</span></code></a> module’s <code class="xref py py-func docutils literal"><span class="pre">strptime()</span></code> function has long been an annoyance
1469
because it uses the platform C library’s <code class="xref py py-func docutils literal"><span class="pre">strptime()</span></code> implementation, and
1470
different platforms sometimes have odd bugs. Brett Cannon contributed a
1471
portable implementation that’s written in pure Python and should behave
1472
identically on all platforms.</p>
1474
<li><p class="first">The new <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><code class="xref py py-mod docutils literal"><span class="pre">timeit</span></code></a> module helps measure how long snippets of Python code
1475
take to execute. The <code class="file docutils literal"><span class="pre">timeit.py</span></code> file can be run directly from the
1476
command line, or the module’s <code class="xref py py-class docutils literal"><span class="pre">Timer</span></code> class can be imported and used
1477
directly. Here’s a short example that figures out whether it’s faster to
1478
convert an 8-bit string to Unicode by appending an empty Unicode string to it or
1479
by using the <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><code class="xref py py-func docutils literal"><span class="pre">unicode()</span></code></a> function:</p>
1480
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">timeit</span>
1482
<span class="n">timer1</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s1">'unicode("abc")'</span><span class="p">)</span>
1483
<span class="n">timer2</span> <span class="o">=</span> <span class="n">timeit</span><span class="o">.</span><span class="n">Timer</span><span class="p">(</span><span class="s1">'"abc" + u""'</span><span class="p">)</span>
1485
<span class="c1"># Run three trials</span>
1486
<span class="k">print</span> <span class="n">timer1</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="mi">100000</span><span class="p">)</span>
1487
<span class="k">print</span> <span class="n">timer2</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="mi">100000</span><span class="p">)</span>
1489
<span class="c1"># On my laptop this outputs:</span>
1490
<span class="c1"># [0.36831796169281006, 0.37441694736480713, 0.35304892063140869]</span>
1491
<span class="c1"># [0.17574405670166016, 0.18193507194519043, 0.17565798759460449]</span>
1495
<li><p class="first">The <a class="reference internal" href="../library/tix.html#module-Tix" title="Tix: Tk Extension Widgets for Tkinter"><code class="xref py py-mod docutils literal"><span class="pre">Tix</span></code></a> module has received various bug fixes and updates for the
1496
current version of the Tix package.</p>
1498
<li><p class="first">The <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></code></a> module now works with a thread-enabled version of Tcl.
1499
Tcl’s threading model requires that widgets only be accessed from the thread in
1500
which they’re created; accesses from another thread can cause Tcl to panic. For
1501
certain Tcl interfaces, <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></code></a> will now automatically avoid this when a
1502
widget is accessed from a different thread by marshalling a command, passing it
1503
to the correct thread, and waiting for the results. Other interfaces can’t be
1504
handled automatically but <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></code></a> will now raise an exception on such an
1505
access so that you can at least find out about the problem. See
1506
<a class="reference external" href="https://mail.python.org/pipermail/python-dev/2002-December/031107.html">https://mail.python.org/pipermail/python-dev/2002-December/031107.html</a> for a more
1507
detailed explanation of this change. (Implemented by Martin von Löwis.)</p>
1509
<li><p class="first">Calling Tcl methods through <code class="xref py py-mod docutils literal"><span class="pre">_tkinter</span></code> no longer returns only strings.
1510
Instead, if Tcl returns other objects those objects are converted to their
1511
Python equivalent, if one exists, or wrapped with a <code class="xref py py-class docutils literal"><span class="pre">_tkinter.Tcl_Obj</span></code>
1512
object if no Python equivalent exists. This behavior can be controlled through
1513
the <code class="xref py py-meth docutils literal"><span class="pre">wantobjects()</span></code> method of <code class="xref py py-class docutils literal"><span class="pre">tkapp</span></code> objects.</p>
1514
<p>When using <code class="xref py py-mod docutils literal"><span class="pre">_tkinter</span></code> through the <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></code></a> module (as most Tkinter
1515
applications will), this feature is always activated. It should not cause
1516
compatibility problems, since Tkinter would always convert string results to
1517
Python types where possible.</p>
1518
<p>If any incompatibilities are found, the old behavior can be restored by setting
1519
the <code class="xref py py-attr docutils literal"><span class="pre">wantobjects</span></code> variable in the <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></code></a> module to false before
1520
creating the first <code class="xref py py-class docutils literal"><span class="pre">tkapp</span></code> object.</p>
1521
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">Tkinter</span>
1522
<span class="n">Tkinter</span><span class="o">.</span><span class="n">wantobjects</span> <span class="o">=</span> <span class="mi">0</span>
1525
<p>Any breakage caused by this change should be reported as a bug.</p>
1527
<li><p class="first">The <a class="reference internal" href="../library/userdict.html#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-mod docutils literal"><span class="pre">UserDict</span></code></a> module has a new <code class="xref py py-class docutils literal"><span class="pre">DictMixin</span></code> class which defines
1528
all dictionary methods for classes that already have a minimum mapping
1529
interface. This greatly simplifies writing classes that need to be
1530
substitutable for dictionaries, such as the classes in the <a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal"><span class="pre">shelve</span></code></a>
1532
<p>Adding the mix-in as a superclass provides the full dictionary interface
1533
whenever the class defines <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></code></a>,
1534
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></code></a>, and <code class="xref py py-meth docutils literal"><span class="pre">keys()</span></code>. For example:</p>
1535
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">UserDict</span>
1536
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">SeqDict</span><span class="p">(</span><span class="n">UserDict</span><span class="o">.</span><span class="n">DictMixin</span><span class="p">):</span>
1537
<span class="gp">... </span> <span class="sd">"""Dictionary lookalike implemented with lists."""</span>
1538
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
1539
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span> <span class="o">=</span> <span class="p">[]</span>
1540
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">valuelist</span> <span class="o">=</span> <span class="p">[]</span>
1541
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
1542
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
1543
<span class="gp">... </span> <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
1544
<span class="gp">... </span> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
1545
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">KeyError</span>
1546
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">valuelist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
1547
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
1548
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
1549
<span class="gp">... </span> <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
1550
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">valuelist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
1551
<span class="gp">... </span> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
1552
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
1553
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">valuelist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
1554
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
1555
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
1556
<span class="gp">... </span> <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
1557
<span class="gp">... </span> <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
1558
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">KeyError</span>
1559
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
1560
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">valuelist</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
1561
<span class="gp">... </span> <span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
1562
<span class="gp">... </span> <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">keylist</span><span class="p">)</span>
1563
<span class="gp">...</span>
1564
<span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SeqDict</span><span class="p">()</span>
1565
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="c1"># See that other dictionary methods are implemented</span>
1566
<span class="go">['__cmp__', '__contains__', '__delitem__', '__doc__', '__getitem__',</span>
1567
<span class="go"> '__init__', '__iter__', '__len__', '__module__', '__repr__',</span>
1568
<span class="go"> '__setitem__', 'clear', 'get', 'has_key', 'items', 'iteritems',</span>
1569
<span class="go"> 'iterkeys', 'itervalues', 'keylist', 'keys', 'pop', 'popitem',</span>
1570
<span class="go"> 'setdefault', 'update', 'valuelist', 'values']</span>
1573
<p>(Contributed by Raymond Hettinger.)</p>
1575
<li><p class="first">The DOM implementation in <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"><span class="pre">xml.dom.minidom</span></code></a> can now generate XML output
1576
in a particular encoding by providing an optional encoding argument to the
1577
<code class="xref py py-meth docutils literal"><span class="pre">toxml()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">toprettyxml()</span></code> methods of DOM nodes.</p>
1579
<li><p class="first">The <a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><code class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></code></a> module now supports an XML-RPC extension for handling nil
1580
data values such as Python’s <code class="docutils literal"><span class="pre">None</span></code>. Nil values are always supported on
1581
unmarshalling an XML-RPC response. To generate requests containing <code class="docutils literal"><span class="pre">None</span></code>,
1582
you must supply a true value for the <em>allow_none</em> parameter when creating a
1583
<code class="xref py py-class docutils literal"><span class="pre">Marshaller</span></code> instance.</p>
1585
<li><p class="first">The new <a class="reference internal" href="../library/docxmlrpcserver.html#module-DocXMLRPCServer" title="DocXMLRPCServer: Self-documenting XML-RPC server implementation."><code class="xref py py-mod docutils literal"><span class="pre">DocXMLRPCServer</span></code></a> module allows writing self-documenting XML-RPC
1586
servers. Run it in demo mode (as a program) to see it in action. Pointing the
1587
Web browser to the RPC server produces pydoc-style documentation; pointing
1588
xmlrpclib to the server allows invoking the actual methods. (Contributed by
1591
<li><p class="first">Support for internationalized domain names (RFCs 3454, 3490, 3491, and 3492)
1592
has been added. The “idna” encoding can be used to convert between a Unicode
1593
domain name and the ASCII-compatible encoding (ACE) of that name.</p>
1594
<div class="highlight-python"><div class="highlight"><pre>>{}>{}> u"www.Alliancefrançaise.nu".encode("idna")
1595
'www.xn--alliancefranaise-npb.nu'
1598
<p>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"><span class="pre">socket</span></code></a> module has also been extended to transparently convert
1599
Unicode hostnames to the ACE version before passing them to the C library.
1600
Modules that deal with hostnames such as <a class="reference internal" href="../library/httplib.html#module-httplib" title="httplib: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">httplib</span></code></a> and <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">ftplib</span></code></a>)
1601
also support Unicode host names; <a class="reference internal" href="../library/httplib.html#module-httplib" title="httplib: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">httplib</span></code></a> also sends HTTP <code class="docutils literal"><span class="pre">Host</span></code>
1602
headers using the ACE version of the domain name. <a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib: Open an arbitrary network resource by URL (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">urllib</span></code></a> supports
1603
Unicode URLs with non-ASCII host names as long as the <code class="docutils literal"><span class="pre">path</span></code> part of the URL
1605
<p>To implement this change, the <a class="reference internal" href="../library/stringprep.html#module-stringprep" title="stringprep: String preparation, as per RFC 3453"><code class="xref py py-mod docutils literal"><span class="pre">stringprep</span></code></a> module, the <code class="docutils literal"><span class="pre">mkstringprep</span></code>
1606
tool and the <code class="docutils literal"><span class="pre">punycode</span></code> encoding have been added.</p>
1609
<div class="section" id="date-time-type">
1610
<h3>Date/Time Type<a class="headerlink" href="#date-time-type" title="Permalink to this headline">¶</a></h3>
1611
<p>Date and time types suitable for expressing timestamps were added as the
1612
<a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal"><span class="pre">datetime</span></code></a> module. The types don’t support different calendars or many
1613
fancy features, and just stick to the basics of representing time.</p>
1614
<p>The three primary types are: <code class="xref py py-class docutils literal"><span class="pre">date</span></code>, representing a day, month, and year;
1615
<a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-class docutils literal"><span class="pre">time</span></code></a>, consisting of hour, minute, and second; and <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal"><span class="pre">datetime</span></code></a>,
1616
which contains all the attributes of both <code class="xref py py-class docutils literal"><span class="pre">date</span></code> and <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-class docutils literal"><span class="pre">time</span></code></a>.
1617
There’s also a <code class="xref py py-class docutils literal"><span class="pre">timedelta</span></code> class representing differences between two
1618
points in time, and time zone logic is implemented by classes inheriting from
1619
the abstract <code class="xref py py-class docutils literal"><span class="pre">tzinfo</span></code> class.</p>
1620
<p>You can create instances of <code class="xref py py-class docutils literal"><span class="pre">date</span></code> and <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-class docutils literal"><span class="pre">time</span></code></a> by either supplying
1621
keyword arguments to the appropriate constructor, e.g.
1622
<code class="docutils literal"><span class="pre">datetime.date(year=1972,</span> <span class="pre">month=10,</span> <span class="pre">day=15)</span></code>, or by using one of a number of
1623
class methods. For example, the <code class="xref py py-meth docutils literal"><span class="pre">date.today()</span></code> class method returns the
1624
current local date.</p>
1625
<p>Once created, instances of the date/time classes are all immutable. There are a
1626
number of methods for producing formatted strings from objects:</p>
1627
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">datetime</span>
1628
<span class="gp">>>> </span><span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
1629
<span class="gp">>>> </span><span class="n">now</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
1630
<span class="go">'2002-12-30T21:27:03.994956'</span>
1631
<span class="gp">>>> </span><span class="n">now</span><span class="o">.</span><span class="n">ctime</span><span class="p">()</span> <span class="c1"># Only available on date, datetime</span>
1632
<span class="go">'Mon Dec 30 21:27:03 2002'</span>
1633
<span class="gp">>>> </span><span class="n">now</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'%Y </span><span class="si">%d</span><span class="s1"> %b'</span><span class="p">)</span>
1634
<span class="go">'2002 30 Dec'</span>
1637
<p>The <code class="xref py py-meth docutils literal"><span class="pre">replace()</span></code> method allows modifying one or more fields of a
1638
<code class="xref py py-class docutils literal"><span class="pre">date</span></code> or <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal"><span class="pre">datetime</span></code></a> instance, returning a new instance:</p>
1639
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
1640
<span class="gp">>>> </span><span class="n">d</span>
1641
<span class="go">datetime.datetime(2002, 12, 30, 22, 15, 38, 827738)</span>
1642
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="mi">2001</span><span class="p">,</span> <span class="n">hour</span> <span class="o">=</span> <span class="mi">12</span><span class="p">)</span>
1643
<span class="go">datetime.datetime(2001, 12, 30, 12, 15, 38, 827738)</span>
1644
<span class="go">>>></span>
1647
<p>Instances can be compared, hashed, and converted to strings (the result is the
1648
same as that of <code class="xref py py-meth docutils literal"><span class="pre">isoformat()</span></code>). <code class="xref py py-class docutils literal"><span class="pre">date</span></code> and <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal"><span class="pre">datetime</span></code></a>
1649
instances can be subtracted from each other, and added to <code class="xref py py-class docutils literal"><span class="pre">timedelta</span></code>
1650
instances. The largest missing feature is that there’s no standard library
1651
support for parsing strings and getting back a <code class="xref py py-class docutils literal"><span class="pre">date</span></code> or
1652
<a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal"><span class="pre">datetime</span></code></a>.</p>
1653
<p>For more information, refer to the module’s reference documentation.
1654
(Contributed by Tim Peters.)</p>
1656
<div class="section" id="the-optparse-module">
1657
<h3>The optparse Module<a class="headerlink" href="#the-optparse-module" title="Permalink to this headline">¶</a></h3>
1658
<p>The <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal"><span class="pre">getopt</span></code></a> module provides simple parsing of command-line arguments. The
1659
new <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a> module (originally named Optik) provides more elaborate
1660
command-line parsing that follows the Unix conventions, automatically creates
1661
the output for <a class="reference internal" href="../using/cmdline.html#cmdoption--help"><code class="xref std std-option docutils literal"><span class="pre">--help</span></code></a>, and can perform different actions for different
1663
<p>You start by creating an instance of <code class="xref py py-class docutils literal"><span class="pre">OptionParser</span></code> and telling it what
1664
your program’s options are.</p>
1665
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
1666
<span class="kn">from</span> <span class="nn">optparse</span> <span class="kn">import</span> <span class="n">OptionParser</span>
1668
<span class="n">op</span> <span class="o">=</span> <span class="n">OptionParser</span><span class="p">()</span>
1669
<span class="n">op</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s1">'-i'</span><span class="p">,</span> <span class="s1">'--input'</span><span class="p">,</span>
1670
<span class="n">action</span><span class="o">=</span><span class="s1">'store'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s1">'string'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'input'</span><span class="p">,</span>
1671
<span class="n">help</span><span class="o">=</span><span class="s1">'set input filename'</span><span class="p">)</span>
1672
<span class="n">op</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s1">'-l'</span><span class="p">,</span> <span class="s1">'--length'</span><span class="p">,</span>
1673
<span class="n">action</span><span class="o">=</span><span class="s1">'store'</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s1">'int'</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">'length'</span><span class="p">,</span>
1674
<span class="n">help</span><span class="o">=</span><span class="s1">'set maximum length of output'</span><span class="p">)</span>
1677
<p>Parsing a command line is then done by calling the <code class="xref py py-meth docutils literal"><span class="pre">parse_args()</span></code> method.</p>
1678
<div class="highlight-python"><div class="highlight"><pre><span class="n">options</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">op</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
1679
<span class="k">print</span> <span class="n">options</span>
1680
<span class="k">print</span> <span class="n">args</span>
1683
<p>This returns an object containing all of the option values, and a list of
1684
strings containing the remaining arguments.</p>
1685
<p>Invoking the script with the various arguments now works as you’d expect it to.
1686
Note that the length argument is automatically converted to an integer.</p>
1687
<div class="highlight-python"><div class="highlight"><pre>$ ./python opt.py -i data arg1
1688
<Values at 0x400cad4c: {'input': 'data', 'length': None}>
1690
$ ./python opt.py --input=data --length=4
1691
<Values at 0x400cad2c: {'input': 'data', 'length': 4}>
1696
<p>The help message is automatically generated for you:</p>
1697
<div class="highlight-python"><div class="highlight"><pre>$ ./python opt.py --help
1698
usage: opt.py [options]
1701
-h, --help show this help message and exit
1702
-iINPUT, --input=INPUT
1704
-lLENGTH, --length=LENGTH
1705
set maximum length of output
1709
<p>See the module’s documentation for more details.</p>
1710
<p>Optik was written by Greg Ward, with suggestions from the readers of the Getopt
1714
<div class="section" id="pymalloc-a-specialized-object-allocator">
1715
<span id="section-pymalloc"></span><h2>Pymalloc: A Specialized Object Allocator<a class="headerlink" href="#pymalloc-a-specialized-object-allocator" title="Permalink to this headline">¶</a></h2>
1716
<p>Pymalloc, a specialized object allocator written by Vladimir Marangozov, was a
1717
feature added to Python 2.1. Pymalloc is intended to be faster than the system
1718
<code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and to have less memory overhead for allocation patterns typical
1719
of Python programs. The allocator uses C’s <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> function to get large
1720
pools of memory and then fulfills smaller memory requests from these pools.</p>
1721
<p>In 2.1 and 2.2, pymalloc was an experimental feature and wasn’t enabled by
1722
default; you had to explicitly enable it when compiling Python by providing the
1723
<code class="xref std std-option docutils literal"><span class="pre">--with-pymalloc</span></code> option to the <strong class="program">configure</strong> script. In 2.3,
1724
pymalloc has had further enhancements and is now enabled by default; you’ll have
1725
to supply <code class="xref std std-option docutils literal"><span class="pre">--without-pymalloc</span></code> to disable it.</p>
1726
<p>This change is transparent to code written in Python; however, pymalloc may
1727
expose bugs in C extensions. Authors of C extension modules should test their
1728
code with pymalloc enabled, because some incorrect code may cause core dumps at
1730
<p>There’s one particularly common error that causes problems. There are a number
1731
of memory allocation functions in Python’s C API that have previously just been
1732
aliases for the C library’s <code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">free()</span></code>, meaning that if
1733
you accidentally called mismatched functions the error wouldn’t be noticeable.
1734
When the object allocator is enabled, these functions aren’t aliases of
1735
<code class="xref c c-func docutils literal"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">free()</span></code> any more, and calling the wrong function to
1736
free memory may get you a core dump. For example, if memory was allocated using
1737
<code class="xref c c-func docutils literal"><span class="pre">PyObject_Malloc()</span></code>, it has to be freed using <code class="xref c c-func docutils literal"><span class="pre">PyObject_Free()</span></code>, not
1738
<code class="xref c c-func docutils literal"><span class="pre">free()</span></code>. A few modules included with Python fell afoul of this and had to
1739
be fixed; doubtless there are more third-party modules that will have the same
1741
<p>As part of this change, the confusing multiple interfaces for allocating memory
1742
have been consolidated down into two API families. Memory allocated with one
1743
family must not be manipulated with functions from the other family. There is
1744
one family for allocating chunks of memory and another family of functions
1745
specifically for allocating Python objects.</p>
1747
<li>To allocate and free an undistinguished chunk of memory use the “raw memory”
1748
family: <a class="reference internal" href="../c-api/memory.html#c.PyMem_Malloc" title="PyMem_Malloc"><code class="xref c c-func docutils literal"><span class="pre">PyMem_Malloc()</span></code></a>, <a class="reference internal" href="../c-api/memory.html#c.PyMem_Realloc" title="PyMem_Realloc"><code class="xref c c-func docutils literal"><span class="pre">PyMem_Realloc()</span></code></a>, and <a class="reference internal" href="../c-api/memory.html#c.PyMem_Free" title="PyMem_Free"><code class="xref c c-func docutils literal"><span class="pre">PyMem_Free()</span></code></a>.</li>
1749
<li>The “object memory” family is the interface to the pymalloc facility described
1750
above and is biased towards a large number of “small” allocations:
1751
<code class="xref c c-func docutils literal"><span class="pre">PyObject_Malloc()</span></code>, <code class="xref c c-func docutils literal"><span class="pre">PyObject_Realloc()</span></code>, and <code class="xref c c-func docutils literal"><span class="pre">PyObject_Free()</span></code>.</li>
1752
<li>To allocate and free Python objects, use the “object” family
1753
<a class="reference internal" href="../c-api/allocation.html#c.PyObject_New" title="PyObject_New"><code class="xref c c-func docutils literal"><span class="pre">PyObject_New()</span></code></a>, <a class="reference internal" href="../c-api/allocation.html#c.PyObject_NewVar" title="PyObject_NewVar"><code class="xref c c-func docutils literal"><span class="pre">PyObject_NewVar()</span></code></a>, and <a class="reference internal" href="../c-api/allocation.html#c.PyObject_Del" title="PyObject_Del"><code class="xref c c-func docutils literal"><span class="pre">PyObject_Del()</span></code></a>.</li>
1755
<p>Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides debugging
1756
features to catch memory overwrites and doubled frees in both extension modules
1757
and in the interpreter itself. To enable this support, compile a debugging
1758
version of the Python interpreter by running <strong class="program">configure</strong> with
1759
<code class="xref std std-option docutils literal"><span class="pre">--with-pydebug</span></code>.</p>
1760
<p>To aid extension writers, a header file <code class="file docutils literal"><span class="pre">Misc/pymemcompat.h</span></code> is
1761
distributed with the source to Python 2.3 that allows Python extensions to use
1762
the 2.3 interfaces to memory allocation while compiling against any version of
1763
Python since 1.5.2. You would copy the file from Python’s source distribution
1764
and bundle it with the source of your extension.</p>
1765
<div class="admonition seealso">
1766
<p class="first admonition-title">See also</p>
1767
<dl class="last docutils">
1768
<dt><a class="reference external" href="https://svn.python.org/view/python/trunk/Objects/obmalloc.c">https://svn.python.org/view/python/trunk/Objects/obmalloc.c</a></dt>
1769
<dd>For the full details of the pymalloc implementation, see the comments at
1770
the top of the file <code class="file docutils literal"><span class="pre">Objects/obmalloc.c</span></code> in the Python source code.
1771
The above link points to the file within the python.org SVN browser.</dd>
1775
<div class="section" id="build-and-c-api-changes">
1776
<h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
1777
<p>Changes to Python’s build process and to the C API include:</p>
1779
<li>The cycle detection implementation used by the garbage collection has proven
1780
to be stable, so it’s now been made mandatory. You can no longer compile Python
1781
without it, and the <code class="xref std std-option docutils literal"><span class="pre">--with-cycle-gc</span></code> switch to <strong class="program">configure</strong> has
1783
<li>Python can now optionally be built as a shared library
1784
(<code class="file docutils literal"><span class="pre">libpython2.3.so</span></code>) by supplying <code class="xref std std-option docutils literal"><span class="pre">--enable-shared</span></code> when running
1785
Python’s <strong class="program">configure</strong> script. (Contributed by Ondrej Palkovsky.)</li>
1786
<li>The <code class="xref c c-macro docutils literal"><span class="pre">DL_EXPORT</span></code> and <code class="xref c c-macro docutils literal"><span class="pre">DL_IMPORT</span></code> macros are now deprecated.
1787
Initialization functions for Python extension modules should now be declared
1788
using the new macro <code class="xref c c-macro docutils literal"><span class="pre">PyMODINIT_FUNC</span></code>, while the Python core will
1789
generally use the <code class="xref c c-macro docutils literal"><span class="pre">PyAPI_FUNC</span></code> and <code class="xref c c-macro docutils literal"><span class="pre">PyAPI_DATA</span></code> macros.</li>
1790
<li>The interpreter can be compiled without any docstrings for the built-in
1791
functions and modules by supplying <code class="xref std std-option docutils literal"><span class="pre">--without-doc-strings</span></code> to the
1792
<strong class="program">configure</strong> script. This makes the Python executable about 10% smaller,
1793
but will also mean that you can’t get help for Python’s built-ins. (Contributed
1794
by Gustavo Niemeyer.)</li>
1795
<li>The <code class="xref c c-func docutils literal"><span class="pre">PyArg_NoArgs()</span></code> macro is now deprecated, and code that uses it
1796
should be changed. For Python 2.2 and later, the method definition table can
1797
specify the <a class="reference internal" href="../c-api/structures.html#METH_NOARGS" title="METH_NOARGS"><code class="xref py py-const docutils literal"><span class="pre">METH_NOARGS</span></code></a> flag, signalling that there are no arguments,
1798
and the argument checking can then be removed. If compatibility with pre-2.2
1799
versions of Python is important, the code could use <code class="docutils literal"><span class="pre">PyArg_ParseTuple(args,</span>
1800
<span class="pre">"")</span></code> instead, but this will be slower than using <a class="reference internal" href="../c-api/structures.html#METH_NOARGS" title="METH_NOARGS"><code class="xref py py-const docutils literal"><span class="pre">METH_NOARGS</span></code></a>.</li>
1801
<li><a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTuple()</span></code></a> accepts new format characters for various sizes of
1802
unsigned integers: <code class="docutils literal"><span class="pre">B</span></code> for <code class="xref c c-type docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></code>, <code class="docutils literal"><span class="pre">H</span></code> for <code class="xref c c-type docutils literal"><span class="pre">unsigned</span>
1803
<span class="pre">short</span> <span class="pre">int</span></code>, <code class="docutils literal"><span class="pre">I</span></code> for <code class="xref c c-type docutils literal"><span class="pre">unsigned</span> <span class="pre">int</span></code>, and <code class="docutils literal"><span class="pre">K</span></code> for <code class="xref c c-type docutils literal"><span class="pre">unsigned</span>
1804
<span class="pre">long</span> <span class="pre">long</span></code>.</li>
1805
<li>A new function, <code class="xref c c-func docutils literal"><span class="pre">PyObject_DelItemString(mapping,</span> <span class="pre">char</span> <span class="pre">*key)()</span></code> was added
1806
as shorthand for <code class="docutils literal"><span class="pre">PyObject_DelItem(mapping,</span> <span class="pre">PyString_New(key))</span></code>.</li>
1807
<li>File objects now manage their internal string buffer differently, increasing
1808
it exponentially when needed. This results in the benchmark tests in
1809
<code class="file docutils literal"><span class="pre">Lib/test/test_bufio.py</span></code> speeding up considerably (from 57 seconds to 1.7
1810
seconds, according to one measurement).</li>
1811
<li>It’s now possible to define class and static methods for a C extension type by
1812
setting either the <a class="reference internal" href="../c-api/structures.html#METH_CLASS" title="METH_CLASS"><code class="xref py py-const docutils literal"><span class="pre">METH_CLASS</span></code></a> or <a class="reference internal" href="../c-api/structures.html#METH_STATIC" title="METH_STATIC"><code class="xref py py-const docutils literal"><span class="pre">METH_STATIC</span></code></a> flags in a
1813
method’s <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal"><span class="pre">PyMethodDef</span></code></a> structure.</li>
1814
<li>Python now includes a copy of the Expat XML parser’s source code, removing any
1815
dependence on a system version or local installation of Expat.</li>
1816
<li>If you dynamically allocate type objects in your extension, you should be
1817
aware of a change in the rules relating to the <code class="xref py py-attr docutils literal"><span class="pre">__module__</span></code> and
1818
<code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code> attributes. In summary, you will want to ensure the type’s
1819
dictionary contains a <code class="docutils literal"><span class="pre">'__module__'</span></code> key; making the module name the part of
1820
the type name leading up to the final period will no longer have the desired
1821
effect. For more detail, read the API reference documentation or the source.</li>
1823
<div class="section" id="port-specific-changes">
1824
<h3>Port-Specific Changes<a class="headerlink" href="#port-specific-changes" title="Permalink to this headline">¶</a></h3>
1825
<p>Support for a port to IBM’s OS/2 using the EMX runtime environment was merged
1826
into the main Python source tree. EMX is a POSIX emulation layer over the OS/2
1827
system APIs. The Python port for EMX tries to support all the POSIX-like
1828
capability exposed by the EMX runtime, and mostly succeeds; <code class="xref py py-func docutils literal"><span class="pre">fork()</span></code> and
1829
<a class="reference internal" href="../library/fcntl.html#module-fcntl" title="fcntl: The fcntl() and ioctl() system calls. (Unix)"><code class="xref py py-func docutils literal"><span class="pre">fcntl()</span></code></a> are restricted by the limitations of the underlying emulation
1830
layer. The standard OS/2 port, which uses IBM’s Visual Age compiler, also
1831
gained support for case-sensitive import semantics as part of the integration of
1832
the EMX port into CVS. (Contributed by Andrew MacIntyre.)</p>
1833
<p>On MacOS, most toolbox modules have been weaklinked to improve backward
1834
compatibility. This means that modules will no longer fail to load if a single
1835
routine is missing on the current OS version. Instead calling the missing
1836
routine will raise an exception. (Contributed by Jack Jansen.)</p>
1837
<p>The RPM spec files, found in the <code class="file docutils literal"><span class="pre">Misc/RPM/</span></code> directory in the Python
1838
source distribution, were updated for 2.3. (Contributed by Sean Reifschneider.)</p>
1839
<p>Other new platforms now supported by Python include AtheOS
1840
(<a class="reference external" href="http://www.atheos.cx/">http://www.atheos.cx/</a>), GNU/Hurd, and OpenVMS.</p>
1843
<div class="section" id="other-changes-and-fixes">
1844
<span id="section-other"></span><h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Permalink to this headline">¶</a></h2>
1845
<p>As usual, there were a bunch of other improvements and bugfixes scattered
1846
throughout the source tree. A search through the CVS change logs finds there
1847
were 523 patches applied and 514 bugs fixed between Python 2.2 and 2.3. Both
1848
figures are likely to be underestimates.</p>
1849
<p>Some of the more notable changes are:</p>
1851
<li><p class="first">If the <span class="target" id="index-29"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONINSPECT"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONINSPECT</span></code></a> environment variable is set, the Python
1852
interpreter will enter the interactive prompt after running a Python program, as
1853
if Python had been invoked with the <a class="reference internal" href="../using/cmdline.html#cmdoption-i"><code class="xref std std-option docutils literal"><span class="pre">-i</span></code></a> option. The environment
1854
variable can be set before running the Python interpreter, or it can be set by
1855
the Python program as part of its execution.</p>
1857
<li><p class="first">The <code class="file docutils literal"><span class="pre">regrtest.py</span></code> script now provides a way to allow “all resources
1858
except <em>foo</em>.” A resource name passed to the <a class="reference internal" href="../using/cmdline.html#cmdoption-u"><code class="xref std std-option docutils literal"><span class="pre">-u</span></code></a> option can now be
1859
prefixed with a hyphen (<code class="docutils literal"><span class="pre">'-'</span></code>) to mean “remove this resource.” For example,
1860
the option ‘<code class="docutils literal"><span class="pre">-uall,-bsddb</span></code>‘ could be used to enable the use of all resources
1861
except <code class="docutils literal"><span class="pre">bsddb</span></code>.</p>
1863
<li><p class="first">The tools used to build the documentation now work under Cygwin as well as
1866
<li><p class="first">The <code class="docutils literal"><span class="pre">SET_LINENO</span></code> opcode has been removed. Back in the mists of time, this
1867
opcode was needed to produce line numbers in tracebacks and support trace
1868
functions (for, e.g., <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal"><span class="pre">pdb</span></code></a>). Since Python 1.5, the line numbers in
1869
tracebacks have been computed using a different mechanism that works with
1870
“python -O”. For Python 2.3 Michael Hudson implemented a similar scheme to
1871
determine when to call the trace function, removing the need for <code class="docutils literal"><span class="pre">SET_LINENO</span></code>
1873
<p>It would be difficult to detect any resulting difference from Python code, apart
1874
from a slight speed up when Python is run without <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal"><span class="pre">-O</span></code></a>.</p>
1875
<p>C extensions that access the <code class="xref py py-attr docutils literal"><span class="pre">f_lineno</span></code> field of frame objects should
1876
instead call <code class="docutils literal"><span class="pre">PyCode_Addr2Line(f->f_code,</span> <span class="pre">f->f_lasti)</span></code>. This will have the
1877
added effect of making the code work as desired under “python -O” in earlier
1878
versions of Python.</p>
1879
<p>A nifty new feature is that trace functions can now assign to the
1880
<code class="xref py py-attr docutils literal"><span class="pre">f_lineno</span></code> attribute of frame objects, changing the line that will be
1881
executed next. A <code class="docutils literal"><span class="pre">jump</span></code> command has been added to the <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal"><span class="pre">pdb</span></code></a> debugger
1882
taking advantage of this new feature. (Implemented by Richie Hindle.)</p>
1886
<div class="section" id="porting-to-python-2-3">
1887
<h2>Porting to Python 2.3<a class="headerlink" href="#porting-to-python-2-3" title="Permalink to this headline">¶</a></h2>
1888
<p>This section lists previously described changes that may require changes to your
1891
<li><p class="first"><a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal"><span class="pre">yield</span></code></a> is now always a keyword; if it’s used as a variable name in
1892
your code, a different name must be chosen.</p>
1894
<li><p class="first">For strings <em>X</em> and <em>Y</em>, <code class="docutils literal"><span class="pre">X</span> <span class="pre">in</span> <span class="pre">Y</span></code> now works if <em>X</em> is more than one
1897
<li><p class="first">The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal"><span class="pre">int()</span></code></a> type constructor will now return a long integer instead of
1898
raising an <a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></code></a> when a string or floating-point number is too
1899
large to fit into an integer.</p>
1901
<li><p class="first">If you have Unicode strings that contain 8-bit characters, you must declare
1902
the file’s encoding (UTF-8, Latin-1, or whatever) by adding a comment to the top
1903
of the file. See section <a class="reference internal" href="#section-encodings"><span>PEP 263: Source Code Encodings</span></a> for more information.</p>
1905
<li><p class="first">Calling Tcl methods through <code class="xref py py-mod docutils literal"><span class="pre">_tkinter</span></code> no longer returns only strings.
1906
Instead, if Tcl returns other objects those objects are converted to their
1907
Python equivalent, if one exists, or wrapped with a <code class="xref py py-class docutils literal"><span class="pre">_tkinter.Tcl_Obj</span></code>
1908
object if no Python equivalent exists.</p>
1910
<li><p class="first">Large octal and hex literals such as <code class="docutils literal"><span class="pre">0xffffffff</span></code> now trigger a
1911
<a class="reference internal" href="../library/exceptions.html#exceptions.FutureWarning" title="exceptions.FutureWarning"><code class="xref py py-exc docutils literal"><span class="pre">FutureWarning</span></code></a>. Currently they’re stored as 32-bit numbers and result in a
1912
negative value, but in Python 2.4 they’ll become positive long integers.</p>
1913
<p>There are a few ways to fix this warning. If you really need a positive number,
1914
just add an <code class="docutils literal"><span class="pre">L</span></code> to the end of the literal. If you’re trying to get a 32-bit
1915
integer with low bits set and have previously used an expression such as <code class="docutils literal"><span class="pre">~(1</span>
1916
<span class="pre"><<</span> <span class="pre">31)</span></code>, it’s probably clearest to start with all bits set and clear the
1917
desired upper bits. For example, to clear just the top bit (bit 31), you could
1918
write <code class="docutils literal"><span class="pre">0xffffffffL</span> <span class="pre">&~(1L<<31)</span></code>.</p>
1920
<li><p class="first">You can no longer disable assertions by assigning to <code class="docutils literal"><span class="pre">__debug__</span></code>.</p>
1922
<li><p class="first">The Distutils <code class="xref py py-func docutils literal"><span class="pre">setup()</span></code> function has gained various new keyword arguments
1923
such as <em>depends</em>. Old versions of the Distutils will abort if passed unknown
1924
keywords. A solution is to check for the presence of the new
1925
<code class="xref py py-func docutils literal"><span class="pre">get_distutil_options()</span></code> function in your <code class="file docutils literal"><span class="pre">setup.py</span></code> and only uses the
1926
new keywords with a version of the Distutils that supports them:</p>
1927
<div class="highlight-python"><div class="highlight"><pre>from distutils import core
1929
kw = {'sources': 'foo.c', ...}
1930
if hasattr(core, 'get_distutil_options'):
1931
kw['depends'] = ['foo.h']
1932
ext = Extension(**kw)
1936
<li><p class="first">Using <code class="docutils literal"><span class="pre">None</span></code> as a variable name will now result in a <a class="reference internal" href="../library/exceptions.html#exceptions.SyntaxWarning" title="exceptions.SyntaxWarning"><code class="xref py py-exc docutils literal"><span class="pre">SyntaxWarning</span></code></a>
1939
<li><p class="first">Names of extension types defined by the modules included with Python now
1940
contain the module and a <code class="docutils literal"><span class="pre">'.'</span></code> in front of the type name.</p>
1944
<div class="section" id="acknowledgements">
1945
<span id="acks"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
1946
<p>The author would like to thank the following people for offering suggestions,
1947
corrections and assistance with various drafts of this article: Jeff Bauer,
1948
Simon Brunning, Brett Cannon, Michael Chermside, Andrew Dalke, Scott David
1949
Daniels, Fred L. Drake, Jr., David Fraser, Kelly Gerber, Raymond Hettinger,
1950
Michael Hudson, Chris Lambert, Detlef Lannert, Martin von Löwis, Andrew
1951
MacIntyre, Lalo Martins, Chad Netzer, Gustavo Niemeyer, Neal Norwitz, Hans
1952
Nowak, Chris Reedy, Francesco Ricciardi, Vinay Sajip, Neil Schemenauer, Roman
1953
Suzi, Jason Tishler, Just van Rossum.</p>
1961
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
1962
<div class="sphinxsidebarwrapper">
1963
<h3><a href="../contents.html">Table Of Contents</a></h3>
1965
<li><a class="reference internal" href="#">What’s New in Python 2.3</a><ul>
1966
<li><a class="reference internal" href="#pep-218-a-standard-set-datatype">PEP 218: A Standard Set Datatype</a></li>
1967
<li><a class="reference internal" href="#pep-255-simple-generators">PEP 255: Simple Generators</a></li>
1968
<li><a class="reference internal" href="#pep-263-source-code-encodings">PEP 263: Source Code Encodings</a></li>
1969
<li><a class="reference internal" href="#pep-273-importing-modules-from-zip-archives">PEP 273: Importing Modules from ZIP Archives</a></li>
1970
<li><a class="reference internal" href="#pep-277-unicode-file-name-support-for-windows-nt">PEP 277: Unicode file name support for Windows NT</a></li>
1971
<li><a class="reference internal" href="#pep-278-universal-newline-support">PEP 278: Universal Newline Support</a></li>
1972
<li><a class="reference internal" href="#pep-279-enumerate">PEP 279: enumerate()</a></li>
1973
<li><a class="reference internal" href="#pep-282-the-logging-package">PEP 282: The logging Package</a></li>
1974
<li><a class="reference internal" href="#pep-285-a-boolean-type">PEP 285: A Boolean Type</a></li>
1975
<li><a class="reference internal" href="#pep-293-codec-error-handling-callbacks">PEP 293: Codec Error Handling Callbacks</a></li>
1976
<li><a class="reference internal" href="#pep-301-package-index-and-metadata-for-distutils">PEP 301: Package Index and Metadata for Distutils</a></li>
1977
<li><a class="reference internal" href="#pep-302-new-import-hooks">PEP 302: New Import Hooks</a></li>
1978
<li><a class="reference internal" href="#pep-305-comma-separated-files">PEP 305: Comma-separated Files</a></li>
1979
<li><a class="reference internal" href="#pep-307-pickle-enhancements">PEP 307: Pickle Enhancements</a></li>
1980
<li><a class="reference internal" href="#extended-slices">Extended Slices</a></li>
1981
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
1982
<li><a class="reference internal" href="#string-changes">String Changes</a></li>
1983
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
1986
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
1987
<li><a class="reference internal" href="#date-time-type">Date/Time Type</a></li>
1988
<li><a class="reference internal" href="#the-optparse-module">The optparse Module</a></li>
1991
<li><a class="reference internal" href="#pymalloc-a-specialized-object-allocator">Pymalloc: A Specialized Object Allocator</a></li>
1992
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
1993
<li><a class="reference internal" href="#port-specific-changes">Port-Specific Changes</a></li>
1996
<li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
1997
<li><a class="reference internal" href="#porting-to-python-2-3">Porting to Python 2.3</a></li>
1998
<li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
2003
<h4>Previous topic</h4>
2004
<p class="topless"><a href="2.4.html"
2005
title="previous chapter">What’s New in Python 2.4</a></p>
2007
<p class="topless"><a href="2.2.html"
2008
title="next chapter">What’s New in Python 2.2</a></p>
2010
<ul class="this-page-menu">
2011
<li><a href="../bugs.html">Report a Bug</a></li>
2012
<li><a href="../_sources/whatsnew/2.3.txt"
2013
rel="nofollow">Show Source</a></li>
2016
<div id="searchbox" style="display: none" role="search">
2017
<h3>Quick search</h3>
2018
<form class="search" action="../search.html" method="get">
2019
<input type="text" name="q" />
2020
<input type="submit" value="Go" />
2021
<input type="hidden" name="check_keywords" value="yes" />
2022
<input type="hidden" name="area" value="default" />
2024
<p class="searchtip" style="font-size: 90%">
2025
Enter search terms or a module, class or function name.
2028
<script type="text/javascript">$('#searchbox').show(0);</script>
2031
<div class="clearer"></div>
2033
<div class="related" role="navigation" aria-label="related navigation">
2036
<li class="right" style="margin-right: 10px">
2037
<a href="../genindex.html" title="General Index"
2040
<a href="../py-modindex.html" title="Python Module Index"
2043
<a href="2.2.html" title="What’s New in Python 2.2"
2046
<a href="2.4.html" title="What’s New in Python 2.4"
2047
>previous</a> |</li>
2048
<li><img src="../_static/py.png" alt=""
2049
style="vertical-align: middle; margin-top: -1px"/></li>
2050
<li><a href="https://www.python.org/">Python</a> »</li>
2052
<span class="version_switcher_placeholder">2.7.11</span>
2053
<a href="../index.html">Documentation</a> »
2056
<li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> »</li>
2059
<div class="footer">
2060
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
2062
The Python Software Foundation is a non-profit corporation.
2063
<a href="https://www.python.org/psf/donations/">Please donate.</a>
2065
Last updated on Jan 23, 2016.
2066
<a href="../bugs.html">Found a bug</a>?
2068
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'