975 lines
117 KiB
HTML
975 lines
117 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>What’s New In Python 3.0 — Python 3.7.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script type="text/javascript" src="../_static/jquery.js"></script>
|
||
<script type="text/javascript" src="../_static/underscore.js"></script>
|
||
<script type="text/javascript" src="../_static/doctools.js"></script>
|
||
<script type="text/javascript" src="../_static/language_data.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="Search within Python 3.7.4 documentation"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="About these documents" href="../about.html" />
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
||
<link rel="next" title="What’s New in Python 2.7" href="2.7.html" />
|
||
<link rel="prev" title="What’s New In Python 3.1" href="3.1.html" />
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.0.html" />
|
||
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/switchers.js"></script>
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
|
||
</head><body>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
accesskey="I">index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="2.7.html" title="What’s New in Python 2.7"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.1.html" title="What’s New In Python 3.1"
|
||
accesskey="P">previous</a> |</li>
|
||
<li><img src="../_static/py.png" alt=""
|
||
style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="what-s-new-in-python-3-0">
|
||
<h1>What’s New In Python 3.0<a class="headerlink" href="#what-s-new-in-python-3-0" title="Permalink to this headline">¶</a></h1>
|
||
<dl class="field-list simple">
|
||
<dt class="field-odd">Author</dt>
|
||
<dd class="field-odd"><p>Guido van Rossum</p>
|
||
</dd>
|
||
</dl>
|
||
<p>This article explains the new features in Python 3.0, compared to 2.6.
|
||
Python 3.0, also known as “Python 3000” or “Py3K”, is the first ever
|
||
<em>intentionally backwards incompatible</em> Python release. There are more
|
||
changes than in a typical release, and more that are important for all
|
||
Python users. Nevertheless, after digesting the changes, you’ll find
|
||
that Python really hasn’t changed all that much – by and large, we’re
|
||
mostly fixing well-known annoyances and warts, and removing a lot of
|
||
old cruft.</p>
|
||
<p>This article doesn’t attempt to provide a complete specification of
|
||
all new features, but instead tries to give a convenient overview.
|
||
For full details, you should refer to the documentation for Python
|
||
3.0, and/or the many PEPs referenced in the text. If you want to
|
||
understand the complete implementation and design rationale for a
|
||
particular feature, PEPs usually have more details than the regular
|
||
documentation; but note that PEPs usually are not kept up-to-date once
|
||
a feature has been fully implemented.</p>
|
||
<p>Due to time constraints this document is not as complete as it should
|
||
have been. As always for a new release, the <code class="docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> file in the
|
||
source distribution contains a wealth of detailed information about
|
||
every small thing that was changed.</p>
|
||
<div class="section" id="common-stumbling-blocks">
|
||
<h2>Common Stumbling Blocks<a class="headerlink" href="#common-stumbling-blocks" title="Permalink to this headline">¶</a></h2>
|
||
<p>This section lists those few changes that are most likely to trip you
|
||
up if you’re used to Python 2.5.</p>
|
||
<div class="section" id="print-is-a-function">
|
||
<h3>Print Is A Function<a class="headerlink" href="#print-is-a-function" title="Permalink to this headline">¶</a></h3>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">print</span></code> statement has been replaced with a <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a>
|
||
function, with keyword arguments to replace most of the special syntax
|
||
of the old <code class="docutils literal notranslate"><span class="pre">print</span></code> statement (<span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a>). Examples:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="s2">"The answer is"</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"The answer is"</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="n">x</span><span class="p">,</span> <span class="c1"># Trailing comma suppresses newline</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s2">" "</span><span class="p">)</span> <span class="c1"># Appends a space instead of a newline</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="c1"># Prints a newline</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">()</span> <span class="c1"># You must call the function!</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="o">>></span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s2">"fatal error"</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"fatal error"</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
|
||
|
||
<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="c1"># prints repr((x, y))</span>
|
||
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span> <span class="c1"># Not the same as print(x, y)!</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can also customize the separator between items, e.g.:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"There are <"</span><span class="p">,</span> <span class="mi">2</span><span class="o">**</span><span class="mi">32</span><span class="p">,</span> <span class="s2">"> possibilities!"</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s2">""</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>which produces:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>There are <4294967296> possibilities!
|
||
</pre></div>
|
||
</div>
|
||
<p>Note:</p>
|
||
<ul class="simple">
|
||
<li><p>The <a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function doesn’t support the “softspace” feature of
|
||
the old <code class="docutils literal notranslate"><span class="pre">print</span></code> statement. For example, in Python 2.x,
|
||
<code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">"A\n",</span> <span class="pre">"B"</span></code> would write <code class="docutils literal notranslate"><span class="pre">"A\nB\n"</span></code>; but in Python 3.0,
|
||
<code class="docutils literal notranslate"><span class="pre">print("A\n",</span> <span class="pre">"B")</span></code> writes <code class="docutils literal notranslate"><span class="pre">"A\n</span> <span class="pre">B\n"</span></code>.</p></li>
|
||
<li><p>Initially, you’ll be finding yourself typing the old <code class="docutils literal notranslate"><span class="pre">print</span> <span class="pre">x</span></code>
|
||
a lot in interactive mode. Time to retrain your fingers to type
|
||
<code class="docutils literal notranslate"><span class="pre">print(x)</span></code> instead!</p></li>
|
||
<li><p>When using the <code class="docutils literal notranslate"><span class="pre">2to3</span></code> source-to-source conversion tool, all
|
||
<code class="docutils literal notranslate"><span class="pre">print</span></code> statements are automatically converted to
|
||
<a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> function calls, so this is mostly a non-issue for
|
||
larger projects.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="views-and-iterators-instead-of-lists">
|
||
<h3>Views And Iterators Instead Of Lists<a class="headerlink" href="#views-and-iterators-instead-of-lists" title="Permalink to this headline">¶</a></h3>
|
||
<p>Some well-known APIs no longer return lists:</p>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> methods <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a> and
|
||
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a> return “views” instead of lists. For example,
|
||
this no longer works: <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">=</span> <span class="pre">d.keys();</span> <span class="pre">k.sort()</span></code>. Use <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">=</span>
|
||
<span class="pre">sorted(d)</span></code> instead (this works in Python 2.5 too and is just
|
||
as efficient).</p></li>
|
||
<li><p>Also, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iterkeys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.iteritems()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.itervalues()</span></code> methods are no longer supported.</p></li>
|
||
<li><p><a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> and <a class="reference internal" href="../library/functions.html#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a> return iterators. If you really need
|
||
a list and the input sequences are all of equal length, a quick
|
||
fix is to wrap <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> in <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a>, e.g. <code class="docutils literal notranslate"><span class="pre">list(map(...))</span></code>,
|
||
but a better fix is
|
||
often to use a list comprehension (especially when the original code
|
||
uses <a class="reference internal" href="../reference/expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a>), or rewriting the code so it doesn’t need a
|
||
list at all. Particularly tricky is <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> invoked for the
|
||
side effects of the function; the correct transformation is to use a
|
||
regular <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> loop (since creating a list would just be
|
||
wasteful).</p>
|
||
<p>If the input sequences are not of equal length, <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> will
|
||
stop at the termination of the shortest of the sequences. For full
|
||
compatibility with <a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a> from Python 2.x, also wrap the sequences in
|
||
<a class="reference internal" href="../library/itertools.html#itertools.zip_longest" title="itertools.zip_longest"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.zip_longest()</span></code></a>, e.g. <code class="docutils literal notranslate"><span class="pre">map(func,</span> <span class="pre">*sequences)</span></code> becomes
|
||
<code class="docutils literal notranslate"><span class="pre">list(map(func,</span> <span class="pre">itertools.zip_longest(*sequences)))</span></code>.</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> now behaves like <code class="xref py py-func docutils literal notranslate"><span class="pre">xrange()</span></code> used to behave, except
|
||
it works with values of arbitrary size. The latter no longer
|
||
exists.</p></li>
|
||
<li><p><a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> now returns an iterator.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="ordering-comparisons">
|
||
<h3>Ordering Comparisons<a class="headerlink" href="#ordering-comparisons" title="Permalink to this headline">¶</a></h3>
|
||
<p>Python 3.0 has simplified the rules for ordering comparisons:</p>
|
||
<ul class="simple">
|
||
<li><p>The ordering comparison operators (<code class="docutils literal notranslate"><span class="pre"><</span></code>, <code class="docutils literal notranslate"><span class="pre"><=</span></code>, <code class="docutils literal notranslate"><span class="pre">>=</span></code>, <code class="docutils literal notranslate"><span class="pre">></span></code>)
|
||
raise a TypeError exception when the operands don’t have a
|
||
meaningful natural ordering. Thus, expressions like <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre"><</span> <span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">0</span>
|
||
<span class="pre">></span> <span class="pre">None</span></code> or <code class="docutils literal notranslate"><span class="pre">len</span> <span class="pre"><=</span> <span class="pre">len</span></code> are no longer valid, and e.g. <code class="docutils literal notranslate"><span class="pre">None</span> <span class="pre"><</span>
|
||
<span class="pre">None</span></code> raises <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> instead of returning
|
||
<code class="docutils literal notranslate"><span class="pre">False</span></code>. A corollary is that sorting a heterogeneous list
|
||
no longer makes sense – all the elements must be comparable to each
|
||
other. Note that this does not apply to the <code class="docutils literal notranslate"><span class="pre">==</span></code> and <code class="docutils literal notranslate"><span class="pre">!=</span></code>
|
||
operators: objects of different incomparable types always compare
|
||
unequal to each other.</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">builtin.sorted()</span></code> and <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> no longer accept the
|
||
<em>cmp</em> argument providing a comparison function. Use the <em>key</em>
|
||
argument instead. N.B. the <em>key</em> and <em>reverse</em> arguments are now
|
||
“keyword-only”.</p></li>
|
||
<li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> function should be treated as gone, and the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code>
|
||
special method is no longer supported. Use <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></a> for sorting,
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> with <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a>, and other rich comparisons as needed.
|
||
(If you really need the <code class="xref py py-func docutils literal notranslate"><span class="pre">cmp()</span></code> functionality, you could use the
|
||
expression <code class="docutils literal notranslate"><span class="pre">(a</span> <span class="pre">></span> <span class="pre">b)</span> <span class="pre">-</span> <span class="pre">(a</span> <span class="pre"><</span> <span class="pre">b)</span></code> as the equivalent for <code class="docutils literal notranslate"><span class="pre">cmp(a,</span> <span class="pre">b)</span></code>.)</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="integers">
|
||
<h3>Integers<a class="headerlink" href="#integers" title="Permalink to this headline">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0237"><strong>PEP 237</strong></a>: Essentially, <code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code> renamed to <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>.
|
||
That is, there is only one built-in integral type, named
|
||
<a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>; but it behaves mostly like the old <code class="xref py py-class docutils literal notranslate"><span class="pre">long</span></code> type.</p></li>
|
||
<li><p><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a>: An expression like <code class="docutils literal notranslate"><span class="pre">1/2</span></code> returns a float. Use
|
||
<code class="docutils literal notranslate"><span class="pre">1//2</span></code> to get the truncating behavior. (The latter syntax has
|
||
existed for years, at least since Python 2.2.)</p></li>
|
||
<li><p>The <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxint</span></code> constant was removed, since there is no
|
||
longer a limit to the value of integers. However, <a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>
|
||
can be used as an integer larger than any practical list or string
|
||
index. It conforms to the implementation’s “natural” integer size
|
||
and is typically the same as <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxint</span></code> in previous releases
|
||
on the same platform (assuming the same build options).</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a long integer doesn’t include the trailing <code class="docutils literal notranslate"><span class="pre">L</span></code>
|
||
anymore, so code that unconditionally strips that character will
|
||
chop off the last digit instead. (Use <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> instead.)</p></li>
|
||
<li><p>Octal literals are no longer of the form <code class="docutils literal notranslate"><span class="pre">0720</span></code>; use <code class="docutils literal notranslate"><span class="pre">0o720</span></code>
|
||
instead.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="text-vs-data-instead-of-unicode-vs-8-bit">
|
||
<h3>Text Vs. Data Instead Of Unicode Vs. 8-bit<a class="headerlink" href="#text-vs-data-instead-of-unicode-vs-8-bit" title="Permalink to this headline">¶</a></h3>
|
||
<p>Everything you thought you knew about binary data and Unicode has
|
||
changed.</p>
|
||
<ul class="simple">
|
||
<li><p>Python 3.0 uses the concepts of <em>text</em> and (binary) <em>data</em> instead
|
||
of Unicode strings and 8-bit strings. All text is Unicode; however
|
||
<em>encoded</em> Unicode is represented as binary data. The type used to
|
||
hold text is <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, the type used to hold data is
|
||
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>. The biggest difference with the 2.x situation is
|
||
that any attempt to mix text and data in Python 3.0 raises
|
||
<a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>, whereas if you were to mix Unicode and 8-bit
|
||
strings in Python 2.x, it would work if the 8-bit string happened to
|
||
contain only 7-bit (ASCII) bytes, but you would get
|
||
<a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a> if it contained non-ASCII values. This
|
||
value-specific behavior has caused numerous sad faces over the
|
||
years.</p></li>
|
||
<li><p>As a consequence of this change in philosophy, pretty much all code
|
||
that uses Unicode, encodings or binary data most likely has to
|
||
change. The change is for the better, as in the 2.x world there
|
||
were numerous bugs having to do with mixing encoded and unencoded
|
||
text. To be prepared in Python 2.x, start using <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code>
|
||
for all unencoded text, and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> for binary or encoded data
|
||
only. Then the <code class="docutils literal notranslate"><span class="pre">2to3</span></code> tool will do most of the work for you.</p></li>
|
||
<li><p>You can no longer use <code class="docutils literal notranslate"><span class="pre">u"..."</span></code> literals for Unicode text.
|
||
However, you must use <code class="docutils literal notranslate"><span class="pre">b"..."</span></code> literals for binary data.</p></li>
|
||
<li><p>As the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> types cannot be mixed, you
|
||
must always explicitly convert between them. Use <a class="reference internal" href="../library/stdtypes.html#str.encode" title="str.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.encode()</span></code></a>
|
||
to go from <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> to <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#bytes.decode" title="bytes.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.decode()</span></code></a>
|
||
to go from <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> to <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. You can also use
|
||
<code class="docutils literal notranslate"><span class="pre">bytes(s,</span> <span class="pre">encoding=...)</span></code> and <code class="docutils literal notranslate"><span class="pre">str(b,</span> <span class="pre">encoding=...)</span></code>,
|
||
respectively.</p></li>
|
||
<li><p>Like <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, the <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type is immutable. There is a
|
||
separate <em>mutable</em> type to hold buffered binary data,
|
||
<a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>. Nearly all APIs that accept <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> also
|
||
accept <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>. The mutable API is based on
|
||
<code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableSequence</span></code>.</p></li>
|
||
<li><p>All backslashes in raw string literals are interpreted literally.
|
||
This means that <code class="docutils literal notranslate"><span class="pre">'\U'</span></code> and <code class="docutils literal notranslate"><span class="pre">'\u'</span></code> escapes in raw strings are not
|
||
treated specially. For example, <code class="docutils literal notranslate"><span class="pre">r'\u20ac'</span></code> is a string of 6
|
||
characters in Python 3.0, whereas in 2.6, <code class="docutils literal notranslate"><span class="pre">ur'\u20ac'</span></code> was the
|
||
single “euro” character. (Of course, this change only affects raw
|
||
string literals; the euro character is <code class="docutils literal notranslate"><span class="pre">'\u20ac'</span></code> in Python 3.0.)</p></li>
|
||
<li><p>The built-in <code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> abstract type was removed. Use
|
||
<a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> instead. The <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> types
|
||
don’t have functionality enough in common to warrant a shared base
|
||
class. The <code class="docutils literal notranslate"><span class="pre">2to3</span></code> tool (see below) replaces every occurrence of
|
||
<code class="xref py py-class docutils literal notranslate"><span class="pre">basestring</span></code> with <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p></li>
|
||
<li><p>Files opened as text files (still the default mode for <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>)
|
||
always use an encoding to map between strings (in memory) and bytes
|
||
(on disk). Binary files (opened with a <code class="docutils literal notranslate"><span class="pre">b</span></code> in the mode argument)
|
||
always use bytes in memory. This means that if a file is opened
|
||
using an incorrect mode or encoding, I/O will likely fail loudly,
|
||
instead of silently producing incorrect data. It also means that
|
||
even Unix users will have to specify the correct mode (text or
|
||
binary) when opening a file. There is a platform-dependent default
|
||
encoding, which on Unixy platforms can be set with the <code class="docutils literal notranslate"><span class="pre">LANG</span></code>
|
||
environment variable (and sometimes also with some other
|
||
platform-specific locale-related environment variables). In many
|
||
cases, but not all, the system default is UTF-8; you should never
|
||
count on this default. Any application reading or writing more than
|
||
pure ASCII text should probably have a way to override the encoding.
|
||
There is no longer any need for using the encoding-aware streams
|
||
in the <a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> module.</p></li>
|
||
<li><p>The initial values of <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> and
|
||
<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> are now unicode-only text files (i.e., they are
|
||
instances of <a class="reference internal" href="../library/io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a>). To read and write bytes data
|
||
with these streams, you need to use their <a class="reference internal" href="../library/io.html#io.TextIOBase.buffer" title="io.TextIOBase.buffer"><code class="xref py py-data docutils literal notranslate"><span class="pre">io.TextIOBase.buffer</span></code></a>
|
||
attribute.</p></li>
|
||
<li><p>Filenames are passed to and returned from APIs as (Unicode) strings.
|
||
This can present platform-specific problems because on some
|
||
platforms filenames are arbitrary byte strings. (On the other hand,
|
||
on Windows filenames are natively stored as Unicode.) As a
|
||
work-around, most APIs (e.g. <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> and many functions in the
|
||
<a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module) that take filenames accept <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects
|
||
as well as strings, and a few APIs have a way to ask for a
|
||
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> return value. Thus, <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> returns a
|
||
list of <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> instances if the argument is a <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>
|
||
instance, and <a class="reference internal" href="../library/os.html#os.getcwdb" title="os.getcwdb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwdb()</span></code></a> returns the current working
|
||
directory as a <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> instance. Note that when
|
||
<a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> returns a list of strings, filenames that
|
||
cannot be decoded properly are omitted rather than raising
|
||
<a class="reference internal" href="../library/exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p></li>
|
||
<li><p>Some system APIs like <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> and <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> can
|
||
also present problems when the bytes made available by the system is
|
||
not interpretable using the default encoding. Setting the <code class="docutils literal notranslate"><span class="pre">LANG</span></code>
|
||
variable and rerunning the program is probably the best approach.</p></li>
|
||
<li><p><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3138"><strong>PEP 3138</strong></a>: The <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a string no longer escapes
|
||
non-ASCII characters. It still escapes control characters and code
|
||
points with non-printable status in the Unicode standard, however.</p></li>
|
||
<li><p><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3120"><strong>PEP 3120</strong></a>: The default source encoding is now UTF-8.</p></li>
|
||
<li><p><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3131"><strong>PEP 3131</strong></a>: Non-ASCII letters are now allowed in identifiers.
|
||
(However, the standard library remains ASCII-only with the exception
|
||
of contributor names in comments.)</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">cStringIO</span></code> modules are gone. Instead,
|
||
import the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module and use <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.StringIO</span></code></a> or
|
||
<a class="reference internal" href="../library/io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> for text and data respectively.</p></li>
|
||
<li><p>See also the <a class="reference internal" href="../howto/unicode.html#unicode-howto"><span class="std std-ref">Unicode HOWTO</span></a>, which was updated for Python 3.0.</p></li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="overview-of-syntax-changes">
|
||
<h2>Overview Of Syntax Changes<a class="headerlink" href="#overview-of-syntax-changes" title="Permalink to this headline">¶</a></h2>
|
||
<p>This section gives a brief overview of every <em>syntactic</em> change in
|
||
Python 3.0.</p>
|
||
<div class="section" id="new-syntax">
|
||
<h3>New Syntax<a class="headerlink" href="#new-syntax" title="Permalink to this headline">¶</a></h3>
|
||
<ul>
|
||
<li><p><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3107"><strong>PEP 3107</strong></a>: Function argument and return value annotations. This
|
||
provides a standardized way of annotating a function’s parameters
|
||
and return value. There are no semantics attached to such
|
||
annotations except that they can be introspected at runtime using
|
||
the <code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code> attribute. The intent is to encourage
|
||
experimentation through metaclasses, decorators or frameworks.</p></li>
|
||
<li><p><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3102"><strong>PEP 3102</strong></a>: Keyword-only arguments. Named parameters occurring
|
||
after <code class="docutils literal notranslate"><span class="pre">*args</span></code> in the parameter list <em>must</em> be specified using
|
||
keyword syntax in the call. You can also use a bare <code class="docutils literal notranslate"><span class="pre">*</span></code> in the
|
||
parameter list to indicate that you don’t accept a variable-length
|
||
argument list, but you do have keyword-only arguments.</p></li>
|
||
<li><p>Keyword arguments are allowed after the list of base classes in a
|
||
class definition. This is used by the new convention for specifying
|
||
a metaclass (see next section), but can be used for other purposes
|
||
as well, as long as the metaclass supports it.</p></li>
|
||
<li><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3104"><strong>PEP 3104</strong></a>: <a class="reference internal" href="../reference/simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> statement. Using <code class="docutils literal notranslate"><span class="pre">nonlocal</span> <span class="pre">x</span></code>
|
||
you can now assign directly to a variable in an outer (but
|
||
non-global) scope. <code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code> is a new reserved word.</p></li>
|
||
<li><p><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3132"><strong>PEP 3132</strong></a>: Extended Iterable Unpacking. You can now write things
|
||
like <code class="docutils literal notranslate"><span class="pre">a,</span> <span class="pre">b,</span> <span class="pre">*rest</span> <span class="pre">=</span> <span class="pre">some_sequence</span></code>. And even <code class="docutils literal notranslate"><span class="pre">*rest,</span> <span class="pre">a</span> <span class="pre">=</span>
|
||
<span class="pre">stuff</span></code>. The <code class="docutils literal notranslate"><span class="pre">rest</span></code> object is always a (possibly empty) list; the
|
||
right-hand side may be any iterable. Example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="n">rest</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This sets <em>a</em> to <code class="docutils literal notranslate"><span class="pre">0</span></code>, <em>b</em> to <code class="docutils literal notranslate"><span class="pre">4</span></code>, and <em>rest</em> to <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>.</p>
|
||
</li>
|
||
<li><p>Dictionary comprehensions: <code class="docutils literal notranslate"><span class="pre">{k:</span> <span class="pre">v</span> <span class="pre">for</span> <span class="pre">k,</span> <span class="pre">v</span> <span class="pre">in</span> <span class="pre">stuff}</span></code> means the
|
||
same thing as <code class="docutils literal notranslate"><span class="pre">dict(stuff)</span></code> but is more flexible. (This is
|
||
<span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0274"><strong>PEP 274</strong></a> vindicated. :-)</p></li>
|
||
<li><p>Set literals, e.g. <code class="docutils literal notranslate"><span class="pre">{1,</span> <span class="pre">2}</span></code>. Note that <code class="docutils literal notranslate"><span class="pre">{}</span></code> is an empty
|
||
dictionary; use <code class="docutils literal notranslate"><span class="pre">set()</span></code> for an empty set. Set comprehensions are
|
||
also supported; e.g., <code class="docutils literal notranslate"><span class="pre">{x</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">stuff}</span></code> means the same thing as
|
||
<code class="docutils literal notranslate"><span class="pre">set(stuff)</span></code> but is more flexible.</p></li>
|
||
<li><p>New octal literals, e.g. <code class="docutils literal notranslate"><span class="pre">0o720</span></code> (already in 2.6). The old octal
|
||
literals (<code class="docutils literal notranslate"><span class="pre">0720</span></code>) are gone.</p></li>
|
||
<li><p>New binary literals, e.g. <code class="docutils literal notranslate"><span class="pre">0b1010</span></code> (already in 2.6), and
|
||
there is a new corresponding built-in function, <a class="reference internal" href="../library/functions.html#bin" title="bin"><code class="xref py py-func docutils literal notranslate"><span class="pre">bin()</span></code></a>.</p></li>
|
||
<li><p>Bytes literals are introduced with a leading <code class="docutils literal notranslate"><span class="pre">b</span></code> or <code class="docutils literal notranslate"><span class="pre">B</span></code>, and
|
||
there is a new corresponding built-in function, <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytes()</span></code></a>.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="changed-syntax">
|
||
<h3>Changed Syntax<a class="headerlink" href="#changed-syntax" title="Permalink to this headline">¶</a></h3>
|
||
<ul>
|
||
<li><p><span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a> and <span class="target" id="index-12"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: new <a class="reference internal" href="../reference/simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> statement syntax:
|
||
<code class="samp docutils literal notranslate"><span class="pre">raise</span> <span class="pre">[</span><em><span class="pre">expr</span></em> <span class="pre">[from</span> <em><span class="pre">expr</span></em><span class="pre">]]</span></code>. See below.</p></li>
|
||
<li><p><code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> and <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> are now reserved words. (Since
|
||
2.6, actually.)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, and <code class="docutils literal notranslate"><span class="pre">None</span></code> are reserved words. (2.6 partially enforced
|
||
the restrictions on <code class="docutils literal notranslate"><span class="pre">None</span></code> already.)</p></li>
|
||
<li><p>Change from <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <em>exc</em>, <em>var</em> to
|
||
<code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code> <em>exc</em> <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em>. See <span class="target" id="index-13"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>.</p></li>
|
||
<li><p><span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a>: New Metaclass Syntax. Instead of:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
|
||
<span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">M</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>you must now use:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">M</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The module-global <code class="xref py py-data docutils literal notranslate"><span class="pre">__metaclass__</span></code> variable is no longer
|
||
supported. (It was a crutch to make it easier to default to
|
||
new-style classes without deriving every class from
|
||
<a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>.)</p>
|
||
</li>
|
||
<li><p>List comprehensions no longer support the syntactic form
|
||
<code class="samp docutils literal notranslate"><span class="pre">[...</span> <span class="pre">for</span> <em><span class="pre">var</span></em> <span class="pre">in</span> <em><span class="pre">item1</span></em><span class="pre">,</span> <em><span class="pre">item2</span></em><span class="pre">,</span> <span class="pre">...]</span></code>. Use
|
||
<code class="samp docutils literal notranslate"><span class="pre">[...</span> <span class="pre">for</span> <em><span class="pre">var</span></em> <span class="pre">in</span> <span class="pre">(</span><em><span class="pre">item1</span></em><span class="pre">,</span> <em><span class="pre">item2</span></em><span class="pre">,</span> <span class="pre">...)]</span></code> instead.
|
||
Also note that list comprehensions have different semantics: they
|
||
are closer to syntactic sugar for a generator expression inside a
|
||
<a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-func docutils literal notranslate"><span class="pre">list()</span></code></a> constructor, and in particular the loop control
|
||
variables are no longer leaked into the surrounding scope.</p></li>
|
||
<li><p>The <em>ellipsis</em> (<code class="docutils literal notranslate"><span class="pre">...</span></code>) can be used as an atomic expression
|
||
anywhere. (Previously it was only allowed in slices.) Also, it
|
||
<em>must</em> now be spelled as <code class="docutils literal notranslate"><span class="pre">...</span></code>. (Previously it could also be
|
||
spelled as <code class="docutils literal notranslate"><span class="pre">.</span> <span class="pre">.</span> <span class="pre">.</span></code>, by a mere accident of the grammar.)</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="removed-syntax">
|
||
<h3>Removed Syntax<a class="headerlink" href="#removed-syntax" title="Permalink to this headline">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-15"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3113"><strong>PEP 3113</strong></a>: Tuple parameter unpacking removed. You can no longer
|
||
write <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">foo(a,</span> <span class="pre">(b,</span> <span class="pre">c)):</span> <span class="pre">...</span></code>.
|
||
Use <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">foo(a,</span> <span class="pre">b_c):</span> <span class="pre">b,</span> <span class="pre">c</span> <span class="pre">=</span> <span class="pre">b_c</span></code> instead.</p></li>
|
||
<li><p>Removed backticks (use <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> instead).</p></li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre"><></span></code> (use <code class="docutils literal notranslate"><span class="pre">!=</span></code> instead).</p></li>
|
||
<li><p>Removed keyword: <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> is no longer a keyword; it remains as
|
||
a function. (Fortunately the function syntax was also accepted in
|
||
2.x.) Also note that <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> no longer takes a stream argument;
|
||
instead of <code class="docutils literal notranslate"><span class="pre">exec(f)</span></code> you can use <code class="docutils literal notranslate"><span class="pre">exec(f.read())</span></code>.</p></li>
|
||
<li><p>Integer literals no longer support a trailing <code class="docutils literal notranslate"><span class="pre">l</span></code> or <code class="docutils literal notranslate"><span class="pre">L</span></code>.</p></li>
|
||
<li><p>String literals no longer support a leading <code class="docutils literal notranslate"><span class="pre">u</span></code> or <code class="docutils literal notranslate"><span class="pre">U</span></code>.</p></li>
|
||
<li><p>The <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> <em>module</em> <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> <code class="docutils literal notranslate"><span class="pre">*</span></code> syntax is only
|
||
allowed at the module level, no longer inside functions.</p></li>
|
||
<li><p>The only acceptable syntax for relative imports is <code class="samp docutils literal notranslate"><span class="pre">from</span> <span class="pre">.[</span><em><span class="pre">module</span></em><span class="pre">]</span>
|
||
<span class="pre">import</span> <em><span class="pre">name</span></em></code>. All <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> forms not starting with <code class="docutils literal notranslate"><span class="pre">.</span></code> are
|
||
interpreted as absolute imports. (<span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a>)</p></li>
|
||
<li><p>Classic classes are gone.</p></li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="changes-already-present-in-python-2-6">
|
||
<h2>Changes Already Present In Python 2.6<a class="headerlink" href="#changes-already-present-in-python-2-6" title="Permalink to this headline">¶</a></h2>
|
||
<p>Since many users presumably make the jump straight from Python 2.5 to
|
||
Python 3.0, this section reminds the reader of new features that were
|
||
originally designed for Python 3.0 but that were back-ported to Python
|
||
2.6. The corresponding sections in <a class="reference internal" href="2.6.html#whats-new-in-2-6"><span class="std std-ref">What’s New in Python 2.6</span></a> should be
|
||
consulted for longer descriptions.</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0343"><span class="std std-ref">PEP 343: The ‘with’ statement</span></a>. The <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement is now a standard
|
||
feature and no longer needs to be imported from the <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>.
|
||
Also check out <a class="reference internal" href="2.6.html#new-26-context-managers"><span class="std std-ref">Writing Context Managers</span></a> and
|
||
<a class="reference internal" href="2.6.html#new-module-contextlib"><span class="std std-ref">The contextlib module</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0366"><span class="std std-ref">PEP 366: Explicit Relative Imports From a Main Module</span></a>. This enhances the usefulness of the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a>
|
||
option when the referenced module lives in a package.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0370"><span class="std std-ref">PEP 370: Per-user site-packages Directory</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-0371"><span class="std std-ref">PEP 371: The multiprocessing Package</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3101"><span class="std std-ref">PEP 3101: Advanced String Formatting</span></a>. Note: the 2.6 description mentions the
|
||
<a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method for both 8-bit and Unicode strings. In 3.0,
|
||
only the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type (text strings with Unicode support)
|
||
supports this method; the <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type does not. The plan is
|
||
to eventually make this the only API for string formatting, and to
|
||
start deprecating the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator in Python 3.1.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3105"><span class="std std-ref">PEP 3105: print As a Function</span></a>. This is now a standard feature and no longer needs
|
||
to be imported from <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a>. More details were given above.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3110"><span class="std std-ref">PEP 3110: Exception-Handling Changes</span></a>. The <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <em>exc</em> <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em>
|
||
syntax is now standard and <code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code> <em>exc</em>, <em>var</em> is no
|
||
longer supported. (Of course, the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> <em>var</em> part is still
|
||
optional.)</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3112"><span class="std std-ref">PEP 3112: Byte Literals</span></a>. The <code class="docutils literal notranslate"><span class="pre">b"..."</span></code> string literal notation (and its
|
||
variants like <code class="docutils literal notranslate"><span class="pre">b'...'</span></code>, <code class="docutils literal notranslate"><span class="pre">b"""..."""</span></code>, and <code class="docutils literal notranslate"><span class="pre">br"..."</span></code>) now
|
||
produces a literal of type <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3116"><span class="std std-ref">PEP 3116: New I/O Library</span></a>. The <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module is now the standard way of
|
||
doing file I/O. The built-in <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function is now an
|
||
alias for <a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a> and has additional keyword arguments
|
||
<em>encoding</em>, <em>errors</em>, <em>newline</em> and <em>closefd</em>. Also note that an
|
||
invalid <em>mode</em> argument now raises <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, not
|
||
<a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>. The binary file object underlying a text file
|
||
object can be accessed as <code class="xref py py-attr docutils literal notranslate"><span class="pre">f.buffer</span></code> (but beware that the
|
||
text object maintains a buffer of itself in order to speed up
|
||
the encoding and decoding operations).</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3118"><span class="std std-ref">PEP 3118: Revised Buffer Protocol</span></a>. The old builtin <code class="xref py py-func docutils literal notranslate"><span class="pre">buffer()</span></code> is now really gone;
|
||
the new builtin <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a> provides (mostly) similar
|
||
functionality.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3119"><span class="std std-ref">PEP 3119: Abstract Base Classes</span></a>. The <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module and the ABCs defined in the
|
||
<a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module plays a somewhat more prominent role in
|
||
the language now, and built-in collection types like <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>
|
||
and <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> conform to the <code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableMapping</span></code>
|
||
and <code class="xref py py-class docutils literal notranslate"><span class="pre">collections.MutableSequence</span></code> ABCs, respectively.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3127"><span class="std std-ref">PEP 3127: Integer Literal Support and Syntax</span></a>. As mentioned above, the new octal literal
|
||
notation is the only one supported, and binary literals have been
|
||
added.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3129"><span class="std std-ref">PEP 3129: Class Decorators</span></a>.</p></li>
|
||
<li><p><a class="reference internal" href="2.6.html#pep-3141"><span class="std std-ref">PEP 3141: A Type Hierarchy for Numbers</span></a>. The <a class="reference internal" href="../library/numbers.html#module-numbers" title="numbers: Numeric abstract base classes (Complex, Real, Integral, etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">numbers</span></code></a> module is another new use of
|
||
ABCs, defining Python’s “numeric tower”. Also note the new
|
||
<a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fractions</span></code></a> module which implements <a class="reference internal" href="../library/numbers.html#numbers.Rational" title="numbers.Rational"><code class="xref py py-class docutils literal notranslate"><span class="pre">numbers.Rational</span></code></a>.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="library-changes">
|
||
<h2>Library Changes<a class="headerlink" href="#library-changes" title="Permalink to this headline">¶</a></h2>
|
||
<p>Due to time constraints, this document does not exhaustively cover the
|
||
very extensive changes to the standard library. <span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> is the
|
||
reference for the major changes to the library. Here’s a capsule
|
||
review:</p>
|
||
<ul>
|
||
<li><p>Many old modules were removed. Some, like <code class="xref py py-mod docutils literal notranslate"><span class="pre">gopherlib</span></code> (no
|
||
longer used) and <code class="xref py py-mod docutils literal notranslate"><span class="pre">md5</span></code> (replaced by <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a>), were
|
||
already deprecated by <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0004"><strong>PEP 4</strong></a>. Others were removed as a result
|
||
of the removal of support for various platforms such as Irix, BeOS
|
||
and Mac OS 9 (see <span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a>). Some modules were also selected for
|
||
removal in Python 3.0 due to lack of use or because a better
|
||
replacement exists. See <span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> for an exhaustive list.</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb3</span></code> package was removed because its presence in the
|
||
core standard library has proved over time to be a particular burden
|
||
for the core developers due to testing instability and Berkeley DB’s
|
||
release schedule. However, the package is alive and well,
|
||
externally maintained at <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">https://www.jcea.es/programacion/pybsddb.htm</a>.</p></li>
|
||
<li><p>Some modules were renamed because their old name disobeyed
|
||
<span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0008"><strong>PEP 8</strong></a>, or for various other reasons. Here’s the list:</p>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 50%" />
|
||
<col style="width: 50%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Old Name</p></th>
|
||
<th class="head"><p>New Name</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p>_winreg</p></td>
|
||
<td><p>winreg</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>ConfigParser</p></td>
|
||
<td><p>configparser</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>copy_reg</p></td>
|
||
<td><p>copyreg</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>Queue</p></td>
|
||
<td><p>queue</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>SocketServer</p></td>
|
||
<td><p>socketserver</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>markupbase</p></td>
|
||
<td><p>_markupbase</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>repr</p></td>
|
||
<td><p>reprlib</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>test.test_support</p></td>
|
||
<td><p>test.support</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</li>
|
||
<li><p>A common pattern in Python 2.x is to have one version of a module
|
||
implemented in pure Python, with an optional accelerated version
|
||
implemented as a C extension; for example, <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> and
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code>. This places the burden of importing the accelerated
|
||
version and falling back on the pure Python version on each user of
|
||
these modules. In Python 3.0, the accelerated versions are
|
||
considered implementation details of the pure Python versions.
|
||
Users should always import the standard version, which attempts to
|
||
import the accelerated version and falls back to the pure Python
|
||
version. The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> / <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> pair received this
|
||
treatment. The <a class="reference internal" href="../library/profile.html#module-profile" title="profile: Python source profiler."><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile</span></code></a> module is on the list for 3.1. The
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code> module has been turned into a class in the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a>
|
||
module.</p></li>
|
||
<li><p>Some related modules have been grouped into packages, and usually
|
||
the submodule names have been simplified. The resulting new
|
||
packages are:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">anydbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">dbhash</span></code>, <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">dumbdbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">gdbm</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">whichdb</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/html.html#module-html" title="html: Helpers for manipulating HTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">HTMLParser</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">htmlentitydefs</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http</span></code></a> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">BaseHTTPServer</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">CGIHTTPServer</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code>).</p></li>
|
||
<li><p><a class="reference internal" href="../library/tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> (all <code class="xref py py-mod docutils literal notranslate"><span class="pre">Tkinter</span></code>-related modules except
|
||
<a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a>). The target audience of <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> doesn’t
|
||
really care about <a class="reference internal" href="../library/tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a>. Also note that as of Python
|
||
2.6, the functionality of <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> has been greatly enhanced.</p></li>
|
||
<li><p><a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a> (<a class="reference internal" href="../library/urllib.html#module-urllib" title="urllib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib</span></code></a>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">robotparse</span></code>).</p></li>
|
||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpc</span></code> (<code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code>, <code class="xref py py-mod docutils literal notranslate"><span class="pre">DocXMLRPCServer</span></code>,
|
||
<code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleXMLRPCServer</span></code>).</p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<p>Some other changes to standard library modules, not covered by
|
||
<span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a>:</p>
|
||
<ul class="simple">
|
||
<li><p>Killed <code class="xref py py-mod docutils literal notranslate"><span class="pre">sets</span></code>. Use the built-in <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> class.</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module: removed <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exitfunc()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_clear()</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_type</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_value</span></code>,
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exc_traceback</span></code>. (Note that <a class="reference internal" href="../library/sys.html#sys.last_type" title="sys.last_type"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.last_type</span></code></a>
|
||
etc. remain.)</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array.array</span></code></a> type: the <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> methods are gone; use <code class="xref py py-meth docutils literal notranslate"><span class="pre">fromfile()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">tofile()</span></code> instead. Also, the <code class="docutils literal notranslate"><span class="pre">'c'</span></code> typecode for array is
|
||
gone – use either <code class="docutils literal notranslate"><span class="pre">'b'</span></code> for bytes or <code class="docutils literal notranslate"><span class="pre">'u'</span></code> for Unicode
|
||
characters.</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/operator.html#module-operator" title="operator: Functions corresponding to the standard operators."><code class="xref py py-mod docutils literal notranslate"><span class="pre">operator</span></code></a> module: removed
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sequenceIncludes()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">isCallable()</span></code>.</p></li>
|
||
<li><p>Cleanup of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> module: <code class="xref py py-func docutils literal notranslate"><span class="pre">acquire_lock()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">release_lock()</span></code> are gone; use <code class="xref py py-func docutils literal notranslate"><span class="pre">acquire()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">release()</span></code> instead.</p></li>
|
||
<li><p>Cleanup of the <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> module: removed the <code class="xref py py-func docutils literal notranslate"><span class="pre">jumpahead()</span></code> API.</p></li>
|
||
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">new</span></code> module is gone.</p></li>
|
||
<li><p>The functions <code class="xref py py-func docutils literal notranslate"><span class="pre">os.tmpnam()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">os.tempnam()</span></code> and
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">os.tmpfile()</span></code> have been removed in favor of the <a class="reference internal" href="../library/tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a>
|
||
module.</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/tokenize.html#module-tokenize" title="tokenize: Lexical scanner for Python source code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tokenize</span></code></a> module has been changed to work with bytes. The
|
||
main entry point is now <a class="reference internal" href="../library/tokenize.html#tokenize.tokenize" title="tokenize.tokenize"><code class="xref py py-func docutils literal notranslate"><span class="pre">tokenize.tokenize()</span></code></a>, instead of
|
||
generate_tokens.</p></li>
|
||
<li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">string.letters</span></code> and its friends (<code class="xref py py-data docutils literal notranslate"><span class="pre">string.lowercase</span></code> and
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">string.uppercase</span></code>) are gone. Use
|
||
<a class="reference internal" href="../library/string.html#string.ascii_letters" title="string.ascii_letters"><code class="xref py py-data docutils literal notranslate"><span class="pre">string.ascii_letters</span></code></a> etc. instead. (The reason for the
|
||
removal is that <code class="xref py py-data docutils literal notranslate"><span class="pre">string.letters</span></code> and friends had
|
||
locale-specific behavior, which is a bad idea for such
|
||
attractively-named global “constants”.)</p></li>
|
||
<li><p>Renamed module <code class="xref py py-mod docutils literal notranslate"><span class="pre">__builtin__</span></code> to <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> (removing the
|
||
underscores, adding an ‘s’). The <code class="xref py py-data docutils literal notranslate"><span class="pre">__builtins__</span></code> variable
|
||
found in most global namespaces is unchanged. To modify a builtin,
|
||
you should use <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a>, not <code class="xref py py-data docutils literal notranslate"><span class="pre">__builtins__</span></code>!</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="pep-3101-a-new-approach-to-string-formatting">
|
||
<h2><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>: A New Approach To String Formatting<a class="headerlink" href="#pep-3101-a-new-approach-to-string-formatting" title="Permalink to this headline">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>A new system for built-in string formatting operations replaces the
|
||
<code class="docutils literal notranslate"><span class="pre">%</span></code> string formatting operator. (However, the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator is
|
||
still supported; it will be deprecated in Python 3.1 and removed
|
||
from the language at some later time.) Read <span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a> for the full
|
||
scoop.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="changes-to-exceptions">
|
||
<h2>Changes To Exceptions<a class="headerlink" href="#changes-to-exceptions" title="Permalink to this headline">¶</a></h2>
|
||
<p>The APIs for raising and catching exception have been cleaned up and
|
||
new powerful features added:</p>
|
||
<ul>
|
||
<li><p><span class="target" id="index-25"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0352"><strong>PEP 352</strong></a>: All exceptions must be derived (directly or indirectly)
|
||
from <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a>. This is the root of the exception
|
||
hierarchy. This is not new as a recommendation, but the
|
||
<em>requirement</em> to inherit from <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> is new. (Python
|
||
2.6 still allowed classic classes to be raised, and placed no
|
||
restriction on what you can catch.) As a consequence, string
|
||
exceptions are finally truly and utterly dead.</p></li>
|
||
<li><p>Almost all exceptions should actually derive from <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>;
|
||
<a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseException</span></code></a> should only be used as a base class for
|
||
exceptions that should only be handled at the top level, such as
|
||
<a class="reference internal" href="../library/exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a> or <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>. The recommended
|
||
idiom for handling all exceptions except for this latter category is
|
||
to use <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a>.</p></li>
|
||
<li><p><code class="xref py py-exc docutils literal notranslate"><span class="pre">StandardError</span></code> was removed.</p></li>
|
||
<li><p>Exceptions no longer behave as sequences. Use the <code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code>
|
||
attribute instead.</p></li>
|
||
<li><p><span class="target" id="index-26"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a>: Raising exceptions. You must now use <code class="samp docutils literal notranslate"><span class="pre">raise</span>
|
||
<em><span class="pre">Exception</span></em><span class="pre">(</span><em><span class="pre">args</span></em><span class="pre">)</span></code> instead of <code class="samp docutils literal notranslate"><span class="pre">raise</span> <em><span class="pre">Exception</span></em><span class="pre">,</span> <em><span class="pre">args</span></em></code>.
|
||
Additionally, you can no longer explicitly specify a traceback;
|
||
instead, if you <em>have</em> to do this, you can assign directly to the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code> attribute (see below).</p></li>
|
||
<li><p><span class="target" id="index-27"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>: Catching exceptions. You must now use
|
||
<code class="samp docutils literal notranslate"><span class="pre">except</span> <em><span class="pre">SomeException</span></em> <span class="pre">as</span> <em><span class="pre">variable</span></em></code> instead
|
||
of <code class="samp docutils literal notranslate"><span class="pre">except</span> <em><span class="pre">SomeException</span></em><span class="pre">,</span> <em><span class="pre">variable</span></em></code>. Moreover, the
|
||
<em>variable</em> is explicitly deleted when the <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> block
|
||
is left.</p></li>
|
||
<li><p><span class="target" id="index-28"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: Exception chaining. There are two cases: implicit
|
||
chaining and explicit chaining. Implicit chaining happens when an
|
||
exception is raised in an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> or <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a>
|
||
handler block. This usually happens due to a bug in the handler
|
||
block; we call this a <em>secondary</em> exception. In this case, the
|
||
original exception (that was being handled) is saved as the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> attribute of the secondary exception.
|
||
Explicit chaining is invoked with this syntax:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">raise</span> <span class="n">SecondaryException</span><span class="p">()</span> <span class="kn">from</span> <span class="nn">primary_exception</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(where <em>primary_exception</em> is any expression that produces an
|
||
exception object, probably an exception that was previously caught).
|
||
In this case, the primary exception is stored on the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> attribute of the secondary exception. The
|
||
traceback printed when an unhandled exception occurs walks the chain
|
||
of <code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code> attributes and prints a
|
||
separate traceback for each component of the chain, with the primary
|
||
exception at the top. (Java users may recognize this behavior.)</p>
|
||
</li>
|
||
<li><p><span class="target" id="index-29"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: Exception objects now store their traceback as the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code> attribute. This means that an exception
|
||
object now contains all the information pertaining to an exception,
|
||
and there are fewer reasons to use <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a> (though the
|
||
latter is not removed).</p></li>
|
||
<li><p>A few exception messages are improved when Windows fails to load an
|
||
extension module. For example, <code class="docutils literal notranslate"><span class="pre">error</span> <span class="pre">code</span> <span class="pre">193</span></code> is now <code class="docutils literal notranslate"><span class="pre">%1</span> <span class="pre">is</span>
|
||
<span class="pre">not</span> <span class="pre">a</span> <span class="pre">valid</span> <span class="pre">Win32</span> <span class="pre">application</span></code>. Strings now deal with non-English
|
||
locales.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="miscellaneous-other-changes">
|
||
<h2>Miscellaneous Other Changes<a class="headerlink" href="#miscellaneous-other-changes" title="Permalink to this headline">¶</a></h2>
|
||
<div class="section" id="operators-and-special-methods">
|
||
<h3>Operators And Special Methods<a class="headerlink" href="#operators-and-special-methods" title="Permalink to this headline">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">!=</span></code> now returns the opposite of <code class="docutils literal notranslate"><span class="pre">==</span></code>, unless <code class="docutils literal notranslate"><span class="pre">==</span></code> returns
|
||
<a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>.</p></li>
|
||
<li><p>The concept of “unbound methods” has been removed from the language.
|
||
When referencing a method as a class attribute, you now get a plain
|
||
function object.</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getslice__()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__setslice__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__delslice__()</span></code>
|
||
were killed. The syntax <code class="docutils literal notranslate"><span class="pre">a[i:j]</span></code> now translates to
|
||
<code class="docutils literal notranslate"><span class="pre">a.__getitem__(slice(i,</span> <span class="pre">j))</span></code> (or <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a> or
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delitem__()</span></code></a>, when used as an assignment or deletion target,
|
||
respectively).</p></li>
|
||
<li><p><span class="target" id="index-30"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3114"><strong>PEP 3114</strong></a>: the standard <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> method has been renamed to
|
||
<a class="reference internal" href="../library/stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a>.</p></li>
|
||
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">__oct__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__hex__()</span></code> special methods are removed
|
||
– <a class="reference internal" href="../library/functions.html#oct" title="oct"><code class="xref py py-func docutils literal notranslate"><span class="pre">oct()</span></code></a> and <a class="reference internal" href="../library/functions.html#hex" title="hex"><code class="xref py py-func docutils literal notranslate"><span class="pre">hex()</span></code></a> use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> now to convert
|
||
the argument to an integer.</p></li>
|
||
<li><p>Removed support for <code class="xref py py-attr docutils literal notranslate"><span class="pre">__members__</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__methods__</span></code>.</p></li>
|
||
<li><p>The function attributes named <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_X</span></code> have been renamed to
|
||
use the <code class="xref py py-data docutils literal notranslate"><span class="pre">__X__</span></code> form, freeing up these names in the function
|
||
attribute namespace for user-defined attributes. To wit,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_closure</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_code</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_defaults</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_dict</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_doc</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">func_globals</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">func_name</span></code> were renamed to <code class="xref py py-attr docutils literal notranslate"><span class="pre">__closure__</span></code>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__code__</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">__defaults__</span></code>, <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a>,
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">__globals__</span></code>, <a class="reference internal" href="../library/stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>,
|
||
respectively.</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">__nonzero__()</span></code> is now <a class="reference internal" href="../reference/datamodel.html#object.__bool__" title="object.__bool__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a>.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="builtins">
|
||
<h3>Builtins<a class="headerlink" href="#builtins" title="Permalink to this headline">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-31"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3135"><strong>PEP 3135</strong></a>: New <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>. You can now invoke <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a>
|
||
without arguments and (assuming this is in a regular instance method
|
||
defined inside a <a class="reference internal" href="../reference/compound_stmts.html#class"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">class</span></code></a> statement) the right class and
|
||
instance will automatically be chosen. With arguments, the behavior
|
||
of <a class="reference internal" href="../library/functions.html#super" title="super"><code class="xref py py-func docutils literal notranslate"><span class="pre">super()</span></code></a> is unchanged.</p></li>
|
||
<li><p><span class="target" id="index-32"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3111"><strong>PEP 3111</strong></a>: <code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code> was renamed to <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a>. That
|
||
is, the new <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> function reads a line from
|
||
<a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> and returns it with the trailing newline stripped.
|
||
It raises <a class="reference internal" href="../library/exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> if the input is terminated prematurely.
|
||
To get the old behavior of <a class="reference internal" href="../library/functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a>, use <code class="docutils literal notranslate"><span class="pre">eval(input())</span></code>.</p></li>
|
||
<li><p>A new built-in function <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> was added to call the
|
||
<a class="reference internal" href="../library/stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> method on an object.</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/functions.html#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> function rounding strategy and return type have
|
||
changed. Exact halfway cases are now rounded to the nearest even
|
||
result instead of away from zero. (For example, <code class="docutils literal notranslate"><span class="pre">round(2.5)</span></code> now
|
||
returns <code class="docutils literal notranslate"><span class="pre">2</span></code> rather than <code class="docutils literal notranslate"><span class="pre">3</span></code>.) <code class="docutils literal notranslate"><span class="pre">round(x[,</span> <span class="pre">n])</span></code> now
|
||
delegates to <code class="docutils literal notranslate"><span class="pre">x.__round__([n])</span></code> instead of always returning a
|
||
float. It generally returns an integer when called with a single
|
||
argument and a value of the same type as <code class="docutils literal notranslate"><span class="pre">x</span></code> when called with two
|
||
arguments.</p></li>
|
||
<li><p>Moved <code class="xref py py-func docutils literal notranslate"><span class="pre">intern()</span></code> to <a class="reference internal" href="../library/sys.html#sys.intern" title="sys.intern"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.intern()</span></code></a>.</p></li>
|
||
<li><p>Removed: <code class="xref py py-func docutils literal notranslate"><span class="pre">apply()</span></code>. Instead of <code class="docutils literal notranslate"><span class="pre">apply(f,</span> <span class="pre">args)</span></code> use
|
||
<code class="docutils literal notranslate"><span class="pre">f(*args)</span></code>.</p></li>
|
||
<li><p>Removed <a class="reference internal" href="../library/functions.html#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a>. Instead of <code class="docutils literal notranslate"><span class="pre">callable(f)</span></code> you can use
|
||
<code class="docutils literal notranslate"><span class="pre">isinstance(f,</span> <span class="pre">collections.Callable)</span></code>. The <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.isCallable()</span></code>
|
||
function is also gone.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">coerce()</span></code>. This function no longer serves a purpose
|
||
now that classic classes are gone.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">execfile()</span></code>. Instead of <code class="docutils literal notranslate"><span class="pre">execfile(fn)</span></code> use
|
||
<code class="docutils literal notranslate"><span class="pre">exec(open(fn).read())</span></code>.</p></li>
|
||
<li><p>Removed the <code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code> type. Use <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>. There are now several
|
||
different kinds of streams that open can return in the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">reduce()</span></code>. Use <a class="reference internal" href="../library/functools.html#functools.reduce" title="functools.reduce"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.reduce()</span></code></a> if you really
|
||
need it; however, 99 percent of the time an explicit <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a>
|
||
loop is more readable.</p></li>
|
||
<li><p>Removed <code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code>. Use <a class="reference internal" href="../library/imp.html#imp.reload" title="imp.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">imp.reload()</span></code></a>.</p></li>
|
||
<li><p>Removed. <code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.has_key()</span></code> – use the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> operator
|
||
instead.</p></li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="build-and-c-api-changes">
|
||
<h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
|
||
<p>Due to time constraints, here is a <em>very</em> incomplete list of changes
|
||
to the C API.</p>
|
||
<ul class="simple">
|
||
<li><p>Support for several platforms was dropped, including but not limited
|
||
to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.</p></li>
|
||
<li><p><span class="target" id="index-33"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a>: New Buffer API.</p></li>
|
||
<li><p><span class="target" id="index-34"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a>: Extension Module Initialization & Finalization.</p></li>
|
||
<li><p><span class="target" id="index-35"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3123"><strong>PEP 3123</strong></a>: Making <a class="reference internal" href="../c-api/structures.html#c.PyObject_HEAD" title="PyObject_HEAD"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_HEAD</span></code></a> conform to standard C.</p></li>
|
||
<li><p>No more C API support for restricted execution.</p></li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Coerce()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_CoerceEx()</span></code>,
|
||
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyMember_Get()</span></code>, and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyMember_Set()</span></code> C APIs are removed.</p></li>
|
||
<li><p>New C API <a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModuleNoBlock" title="PyImport_ImportModuleNoBlock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleNoBlock()</span></code></a>, works like
|
||
<a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a> but won’t block on the import lock
|
||
(returning an error instead).</p></li>
|
||
<li><p>Renamed the boolean conversion C-level slot and method:
|
||
<code class="docutils literal notranslate"><span class="pre">nb_nonzero</span></code> is now <code class="docutils literal notranslate"><span class="pre">nb_bool</span></code>.</p></li>
|
||
<li><p>Removed <code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_OLDARGS</span></code> and <code class="xref c c-macro docutils literal notranslate"><span class="pre">WITH_CYCLE_GC</span></code> from the C API.</p></li>
|
||
</ul>
|
||
</div>
|
||
<div class="section" id="performance">
|
||
<h2>Performance<a class="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
|
||
<p>The net result of the 3.0 generalizations is that Python 3.0 runs the
|
||
pystone benchmark around 10% slower than Python 2.5. Most likely the
|
||
biggest cause is the removal of special-casing for small integers.
|
||
There’s room for improvement, but it will happen after 3.0 is
|
||
released!</p>
|
||
</div>
|
||
<div class="section" id="porting-to-python-3-0">
|
||
<h2>Porting To Python 3.0<a class="headerlink" href="#porting-to-python-3-0" title="Permalink to this headline">¶</a></h2>
|
||
<p>For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
|
||
best strategy is the following:</p>
|
||
<ol class="arabic simple" start="0">
|
||
<li><p>(Prerequisite:) Start with excellent test coverage.</p></li>
|
||
<li><p>Port to Python 2.6. This should be no more work than the average
|
||
port from Python 2.x to Python 2.(x+1). Make sure all your tests
|
||
pass.</p></li>
|
||
<li><p>(Still using 2.6:) Turn on the <code class="xref std std-option docutils literal notranslate"><span class="pre">-3</span></code> command line switch.
|
||
This enables warnings about features that will be removed (or
|
||
change) in 3.0. Run your test suite again, and fix code that you
|
||
get warnings about until there are no warnings left, and all your
|
||
tests still pass.</p></li>
|
||
<li><p>Run the <code class="docutils literal notranslate"><span class="pre">2to3</span></code> source-to-source translator over your source code
|
||
tree. (See <a class="reference internal" href="../library/2to3.html#to3-reference"><span class="std std-ref">2to3 - Automated Python 2 to 3 code translation</span></a> for more on this tool.) Run the
|
||
result of the translation under Python 3.0. Manually fix up any
|
||
remaining issues, fixing problems until all tests pass again.</p></li>
|
||
</ol>
|
||
<p>It is not recommended to try to write source code that runs unchanged
|
||
under both Python 2.6 and 3.0; you’d have to use a very contorted
|
||
coding style, e.g. avoiding <code class="docutils literal notranslate"><span class="pre">print</span></code> statements, metaclasses,
|
||
and much more. If you are maintaining a library that needs to support
|
||
both Python 2.6 and Python 3.0, the best approach is to modify step 3
|
||
above by editing the 2.6 version of the source code and running the
|
||
<code class="docutils literal notranslate"><span class="pre">2to3</span></code> translator again, rather than editing the 3.0 version of the
|
||
source code.</p>
|
||
<p>For porting C extensions to Python 3.0, please see <a class="reference internal" href="../howto/cporting.html#cporting-howto"><span class="std std-ref">Porting Extension Modules to Python 3</span></a>.</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">Table of Contents</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">What’s New In Python 3.0</a><ul>
|
||
<li><a class="reference internal" href="#common-stumbling-blocks">Common Stumbling Blocks</a><ul>
|
||
<li><a class="reference internal" href="#print-is-a-function">Print Is A Function</a></li>
|
||
<li><a class="reference internal" href="#views-and-iterators-instead-of-lists">Views And Iterators Instead Of Lists</a></li>
|
||
<li><a class="reference internal" href="#ordering-comparisons">Ordering Comparisons</a></li>
|
||
<li><a class="reference internal" href="#integers">Integers</a></li>
|
||
<li><a class="reference internal" href="#text-vs-data-instead-of-unicode-vs-8-bit">Text Vs. Data Instead Of Unicode Vs. 8-bit</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#overview-of-syntax-changes">Overview Of Syntax Changes</a><ul>
|
||
<li><a class="reference internal" href="#new-syntax">New Syntax</a></li>
|
||
<li><a class="reference internal" href="#changed-syntax">Changed Syntax</a></li>
|
||
<li><a class="reference internal" href="#removed-syntax">Removed Syntax</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#changes-already-present-in-python-2-6">Changes Already Present In Python 2.6</a></li>
|
||
<li><a class="reference internal" href="#library-changes">Library Changes</a></li>
|
||
<li><a class="reference internal" href="#pep-3101-a-new-approach-to-string-formatting"><strong>PEP 3101</strong>: A New Approach To String Formatting</a></li>
|
||
<li><a class="reference internal" href="#changes-to-exceptions">Changes To Exceptions</a></li>
|
||
<li><a class="reference internal" href="#miscellaneous-other-changes">Miscellaneous Other Changes</a><ul>
|
||
<li><a class="reference internal" href="#operators-and-special-methods">Operators And Special Methods</a></li>
|
||
<li><a class="reference internal" href="#builtins">Builtins</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a></li>
|
||
<li><a class="reference internal" href="#performance">Performance</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-0">Porting To Python 3.0</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="3.1.html"
|
||
title="previous chapter">What’s New In Python 3.1</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="2.7.html"
|
||
title="next chapter">What’s New in Python 2.7</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.7/Doc/whatsnew/3.0.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
>index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="2.7.html" title="What’s New in Python 2.7"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.1.html" title="What’s New In Python 3.1"
|
||
>previous</a> |</li>
|
||
<li><img src="../_static/py.png" alt=""
|
||
style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
||
<br />
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
Last updated on Jul 13, 2019.
|
||
<a href="../bugs.html">Found a bug</a>?
|
||
<br />
|
||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |