Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 863

python3-docs-3.1.1-2mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>What’s New In Python 3.0 &mdash; Python v3.1.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.1.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.1.1 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.1.1 documentation" href="../index.html" />
    <link rel="up" title="What’s New in Python" href="index.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" />
 

  </head>
  <body>
    <div class="related">
      <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="../modindex.html" title="Global Module Index"
             accesskey="M">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="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="what-s-new-in-python-3-0">
<h1>What&#8217;s New In Python 3.0<a class="headerlink" href="#what-s-new-in-python-3-0" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">Guido van Rossum</td>
</tr>
<tr class="field"><th class="field-name">Release:</th><td class="field-body">3.1.1</td>
</tr>
<tr class="field"><th class="field-name">Date:</th><td class="field-body">August 16, 2009</td>
</tr>
</tbody>
</table>
<p>This article explains the new features in Python 3.0, compared to 2.6.
Python 3.0, also known as &#8220;Python 3000&#8221; or &#8220;Py3K&#8221;, 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&#8217;ll find
that Python really hasn&#8217;t changed all that much &#8211; by and large, we&#8217;re
mostly fixing well-known annoyances and warts, and removing a lot of
old cruft.</p>
<p>This article doesn&#8217;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 <tt class="docutils literal"><span class="pre">Misc/NEWS</span></tt> 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&#8217;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 <tt class="xref docutils literal"><span class="pre">print</span></tt> statement has been replaced with a <a title="print" class="reference external" href="../library/functions.html#print"><tt class="xref docutils literal"><span class="pre">print()</span></tt></a>
function, with keyword arguments to replace most of the special syntax
of the old <tt class="xref docutils literal"><span class="pre">print</span></tt> statement (<span class="target" id="index-1144"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a>).  Examples:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="s">&quot;The answer is&quot;</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="s">&quot;The answer is&quot;</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="c"># 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="s">&quot; &quot;</span><span class="p">)</span>  <span class="c"># Appends a space instead of a newline</span>

<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span>              <span class="c"># Prints a newline</span>
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">()</span>            <span class="c"># You must call the function!</span>

<span class="n">Old</span><span class="p">:</span> <span class="nb">print</span> <span class="o">&gt;&gt;</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;fatal error&quot;</span>
<span class="n">New</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s">&quot;fatal error&quot;</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="c"># 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="c"># 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"><div class="highlight"><pre><span class="nb">print</span><span class="p">(</span><span class="s">&quot;There are &lt;&quot;</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="s">&quot;&gt; possibilities!&quot;</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>which produces:</p>
<div class="highlight-python3"><pre>There are &lt;4294967296&gt; possibilities!</pre>
</div>
<p>Note:</p>
<ul class="simple">
<li>The <a title="print" class="reference external" href="../library/functions.html#print"><tt class="xref docutils literal"><span class="pre">print()</span></tt></a> function doesn&#8217;t support the &#8220;softspace&#8221; feature of
the old <tt class="xref docutils literal"><span class="pre">print</span></tt> statement.  For example, in Python 2.x,
<tt class="docutils literal"><span class="pre">print</span> <span class="pre">&quot;A\n&quot;,</span> <span class="pre">&quot;B&quot;</span></tt> would write <tt class="docutils literal"><span class="pre">&quot;A\nB\n&quot;</span></tt>; but in Python 3.0,
<tt class="docutils literal"><span class="pre">print(&quot;A\n&quot;,</span> <span class="pre">&quot;B&quot;)</span></tt> writes <tt class="docutils literal"><span class="pre">&quot;A\n</span> <span class="pre">B\n&quot;</span></tt>.</li>
<li>Initially, you&#8217;ll be finding yourself typing the old <tt class="docutils literal"><span class="pre">print</span> <span class="pre">x</span></tt>
a lot in interactive mode.  Time to retrain your fingers to type
<tt class="docutils literal"><span class="pre">print(x)</span></tt> instead!</li>
<li>When using the <tt class="docutils literal"><span class="pre">2to3</span></tt> source-to-source conversion tool, all
<tt class="xref docutils literal"><span class="pre">print</span></tt> statements are automatically converted to
<a title="print" class="reference external" href="../library/functions.html#print"><tt class="xref docutils literal"><span class="pre">print()</span></tt></a> function calls, so this is mostly a non-issue for
larger projects.</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 class="simple">
<li><a title="dict" class="reference external" href="../library/stdtypes.html#dict"><tt class="xref docutils literal"><span class="pre">dict</span></tt></a> methods <a title="dict.keys" class="reference external" href="../library/stdtypes.html#dict.keys"><tt class="xref docutils literal"><span class="pre">dict.keys()</span></tt></a>, <a title="dict.items" class="reference external" href="../library/stdtypes.html#dict.items"><tt class="xref docutils literal"><span class="pre">dict.items()</span></tt></a> and
<a title="dict.values" class="reference external" href="../library/stdtypes.html#dict.values"><tt class="xref docutils literal"><span class="pre">dict.values()</span></tt></a> return &#8220;views&#8221; instead of lists.  For example,
this no longer works: <tt class="docutils literal"><span class="pre">k</span> <span class="pre">=</span> <span class="pre">d.keys();</span> <span class="pre">k.sort()</span></tt>.  Use <tt class="docutils literal"><span class="pre">k</span> <span class="pre">=</span>
<span class="pre">sorted(d)</span></tt> instead (this works in Python 2.5 too and is just
as efficient).</li>
<li>Also, the <tt class="xref docutils literal"><span class="pre">dict.iterkeys()</span></tt>, <tt class="xref docutils literal"><span class="pre">dict.iteritems()</span></tt> and
<tt class="xref docutils literal"><span class="pre">dict.itervalues()</span></tt> methods are no longer supported.</li>
<li><a title="map" class="reference external" href="../library/functions.html#map"><tt class="xref docutils literal"><span class="pre">map()</span></tt></a> and <a title="filter" class="reference external" href="../library/functions.html#filter"><tt class="xref docutils literal"><span class="pre">filter()</span></tt></a> return iterators.  If you really need
a list, a quick fix is e.g.  <tt class="docutils literal"><span class="pre">list(map(...))</span></tt>, but a better fix is
often to use a list comprehension (especially when the original code
uses <a class="reference external" href="../reference/expressions.html#lambda"><tt class="xref docutils literal"><span class="pre">lambda</span></tt></a>), or rewriting the code so it doesn&#8217;t need a
list at all.  Particularly tricky is <a title="map" class="reference external" href="../library/functions.html#map"><tt class="xref docutils literal"><span class="pre">map()</span></tt></a> invoked for the
side effects of the function; the correct transformation is to use a
regular <a class="reference external" href="../reference/compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a> loop (since creating a list would just be
wasteful).</li>
<li><a title="range" class="reference external" href="../library/functions.html#range"><tt class="xref docutils literal"><span class="pre">range()</span></tt></a> now behaves like <tt class="xref docutils literal"><span class="pre">xrange()</span></tt> used to behave, except
it works with values of arbitrary size.  The latter no longer
exists.</li>
<li><a title="zip" class="reference external" href="../library/functions.html#zip"><tt class="xref docutils literal"><span class="pre">zip()</span></tt></a> now returns an iterator.</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>The ordering comparison operators (<tt class="docutils literal"><span class="pre">&lt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>)
raise a TypeError exception when the operands don&#8217;t have a
meaningful natural ordering.  Thus, expressions like <tt class="docutils literal"><span class="pre">1</span> <span class="pre">&lt;</span> <span class="pre">''</span></tt>, <tt class="docutils literal"><span class="pre">0</span>
<span class="pre">&gt;</span> <span class="pre">None</span></tt> or <tt class="docutils literal"><span class="pre">len</span> <span class="pre">&lt;=</span> <span class="pre">len</span></tt> are no longer valid, and e.g. <tt class="docutils literal"><span class="pre">None</span> <span class="pre">&lt;</span>
<span class="pre">None</span></tt> raises <a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> instead of returning
<tt class="xref xref docutils literal"><span class="pre">False</span></tt>.  A corollary is that sorting a heterogeneous list
no longer makes sense &#8211; all the elements must be comparable to each
other.  Note that this does not apply to the <tt class="docutils literal"><span class="pre">==</span></tt> and <tt class="docutils literal"><span class="pre">!=</span></tt>
operators: objects of different incomparable types always compare
unequal to each other.</li>
<li><tt class="xref docutils literal"><span class="pre">builtin.sorted()</span></tt> and <tt class="xref docutils literal"><span class="pre">list.sort()</span></tt> 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
&#8220;keyword-only&#8221;.</li>
<li>The <tt class="xref docutils literal"><span class="pre">cmp()</span></tt> function should be treated as gone, and the <tt class="xref docutils literal"><span class="pre">__cmp__()</span></tt>
special method is no longer supported.  Use <a title="object.__lt__" class="reference external" href="../reference/datamodel.html#object.__lt__"><tt class="xref docutils literal"><span class="pre">__lt__()</span></tt></a> for sorting,
<a title="object.__eq__" class="reference external" href="../reference/datamodel.html#object.__eq__"><tt class="xref docutils literal"><span class="pre">__eq__()</span></tt></a> with <a title="object.__hash__" class="reference external" href="../reference/datamodel.html#object.__hash__"><tt class="xref docutils literal"><span class="pre">__hash__()</span></tt></a>, and other rich comparisons as needed.
(If you really need the <tt class="xref docutils literal"><span class="pre">cmp()</span></tt> functionality, you could use the
expression <tt class="docutils literal"><span class="pre">(a</span> <span class="pre">&gt;</span> <span class="pre">b)</span> <span class="pre">-</span> <span class="pre">(a</span> <span class="pre">&lt;</span> <span class="pre">b)</span></tt> as the equivalent for <tt class="docutils literal"><span class="pre">cmp(a,</span> <span class="pre">b)</span></tt>.)</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><span class="target" id="index-1145"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0237"><strong>PEP 0237</strong></a>: Essentially, <tt class="xref docutils literal"><span class="pre">long</span></tt> renamed to <a title="int" class="reference external" href="../library/functions.html#int"><tt class="xref docutils literal"><span class="pre">int</span></tt></a>.
That is, there is only one built-in integral type, named
<a title="int" class="reference external" href="../library/functions.html#int"><tt class="xref docutils literal"><span class="pre">int</span></tt></a>; but it behaves mostly like the old <tt class="xref docutils literal"><span class="pre">long</span></tt> type.</li>
<li><span class="target" id="index-1146"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0238"><strong>PEP 0238</strong></a>: An expression like <tt class="docutils literal"><span class="pre">1/2</span></tt> returns a float.  Use
<tt class="docutils literal"><span class="pre">1//2</span></tt> to get the truncating behavior.  (The latter syntax has
existed for years, at least since Python 2.2.)</li>
<li>The <tt class="xref docutils literal"><span class="pre">sys.maxint</span></tt> constant was removed, since there is no
longer a limit to the value of integers.  However, <a title="sys.maxsize" class="reference external" href="../library/sys.html#sys.maxsize"><tt class="xref docutils literal"><span class="pre">sys.maxsize</span></tt></a>
can be used as an integer larger than any practical list or string
index.  It conforms to the implementation&#8217;s &#8220;natural&#8221; integer size
and is typically the same as <tt class="xref docutils literal"><span class="pre">sys.maxint</span></tt> in previous releases
on the same platform (assuming the same build options).</li>
<li>The <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> of a long integer doesn&#8217;t include the trailing <tt class="docutils literal"><span class="pre">L</span></tt>
anymore, so code that unconditionally strips that character will
chop off the last digit instead.  (Use <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a> instead.)</li>
<li>Octal literals are no longer of the form <tt class="docutils literal"><span class="pre">0720</span></tt>; use <tt class="docutils literal"><span class="pre">0o720</span></tt>
instead.</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>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 title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a>, the type used to hold data is
<a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a>.  The biggest difference with the 2.x situation is
that any attempt to mix text and data in Python 3.0 raises
<a title="exceptions.TypeError" class="reference external" href="../library/exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></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 title="exceptions.UnicodeDecodeError" class="reference external" href="../library/exceptions.html#exceptions.UnicodeDecodeError"><tt class="xref docutils literal"><span class="pre">UnicodeDecodeError</span></tt></a> if it contained non-ASCII values.  This
value-specific behavior has caused numerous sad faces over the
years.</li>
<li>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 <tt class="xref docutils literal"><span class="pre">unicode</span></tt>
for all unencoded text, and <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> for binary or encoded data
only.  Then the <tt class="docutils literal"><span class="pre">2to3</span></tt> tool will do most of the work for you.</li>
<li>You can no longer use <tt class="docutils literal"><span class="pre">u&quot;...&quot;</span></tt> literals for Unicode text.
However, you must use <tt class="docutils literal"><span class="pre">b&quot;...&quot;</span></tt> literals for binary data.</li>
<li>As the <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> and <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> types cannot be mixed, you
must always explicitly convert between them.  Use <a title="str.encode" class="reference external" href="../library/stdtypes.html#str.encode"><tt class="xref docutils literal"><span class="pre">str.encode()</span></tt></a>
to go from <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> to <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a>, and <a title="bytes.decode" class="reference external" href="../library/stdtypes.html#bytes.decode"><tt class="xref docutils literal"><span class="pre">bytes.decode()</span></tt></a>
to go from <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> to <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a>.  You can also use
<tt class="docutils literal"><span class="pre">bytes(s,</span> <span class="pre">encoding=...)</span></tt> and <tt class="docutils literal"><span class="pre">str(b,</span> <span class="pre">encoding=...)</span></tt>,
respectively.</li>
<li>Like <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a>, the <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> type is immutable.  There is a
separate <em>mutable</em> type to hold buffered binary data,
<a title="bytearray" class="reference external" href="../library/functions.html#bytearray"><tt class="xref docutils literal"><span class="pre">bytearray</span></tt></a>.  Nearly all APIs that accept <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> also
accept <a title="bytearray" class="reference external" href="../library/functions.html#bytearray"><tt class="xref docutils literal"><span class="pre">bytearray</span></tt></a>.  The mutable API is based on
<tt class="xref docutils literal"><span class="pre">collections.MutableSequence</span></tt>.</li>
<li>All backslashes in raw string literals are interpreted literally.
This means that <tt class="docutils literal"><span class="pre">'\U'</span></tt> and <tt class="docutils literal"><span class="pre">'\u'</span></tt> escapes in raw strings are not
treated specially.  For example, <tt class="docutils literal"><span class="pre">r'\u20ac'</span></tt> is a string of 6
characters in Python 3.0, whereas in 2.6, <tt class="docutils literal"><span class="pre">ur'\u20ac'</span></tt> was the
single &#8220;euro&#8221; character.  (Of course, this change only affects raw
string literals; the euro character is <tt class="docutils literal"><span class="pre">'\u20ac'</span></tt> in Python 3.0.)</li>
<li>The builtin <tt class="xref docutils literal"><span class="pre">basestring</span></tt> abstract type was removed.  Use
<a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> instead.  The <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> and <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> types
don&#8217;t have functionality enough in common to warrant a shared base
class.  The <tt class="docutils literal"><span class="pre">2to3</span></tt> tool (see below) replaces every occurrence of
<tt class="xref docutils literal"><span class="pre">basestring</span></tt> with <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a>.</li>
<li>Files opened as text files (still the default mode for <a title="open" class="reference external" href="../library/functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a>)
always use an encoding to map between strings (in memory) and bytes
(on disk).  Binary files (opened with a <tt class="docutils literal"><span class="pre">b</span></tt> 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 <tt class="docutils literal"><span class="pre">LANG</span></tt>
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 title="Encode and decode data and streams." class="reference external" href="../library/codecs.html#module-codecs"><tt class="xref docutils literal"><span class="pre">codecs</span></tt></a> module.</li>
<li>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 title="open" class="reference external" href="../library/functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> and many functions in the
<a title="Miscellaneous operating system interfaces." class="reference external" href="../library/os.html#module-os"><tt class="xref docutils literal"><span class="pre">os</span></tt></a> module) that take filenames accept <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> objects
as well as strings, and a few APIs have a way to ask for a
<a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> return value.  Thus, <a title="os.listdir" class="reference external" href="../library/os.html#os.listdir"><tt class="xref docutils literal"><span class="pre">os.listdir()</span></tt></a> returns a
list of <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> instances if the argument is a <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a>
instance, and <a title="os.getcwdb" class="reference external" href="../library/os.html#os.getcwdb"><tt class="xref docutils literal"><span class="pre">os.getcwdb()</span></tt></a> returns the current working
directory as a <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> instance.  Note that when
<a title="os.listdir" class="reference external" href="../library/os.html#os.listdir"><tt class="xref docutils literal"><span class="pre">os.listdir()</span></tt></a> returns a list of strings, filenames that
cannot be decoded properly are omitted rather than raising
<a title="exceptions.UnicodeError" class="reference external" href="../library/exceptions.html#exceptions.UnicodeError"><tt class="xref docutils literal"><span class="pre">UnicodeError</span></tt></a>.</li>
<li>Some system APIs like <a title="os.environ" class="reference external" href="../library/os.html#os.environ"><tt class="xref docutils literal"><span class="pre">os.environ</span></tt></a> and <a title="sys.argv" class="reference external" href="../library/sys.html#sys.argv"><tt class="xref docutils literal"><span class="pre">sys.argv</span></tt></a> can
also present problems when the bytes made available by the system is
not interpretable using the default encoding.  Setting the <tt class="docutils literal"><span class="pre">LANG</span></tt>
variable and rerunning the program is probably the best approach.</li>
<li><span class="target" id="index-1147"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3138"><strong>PEP 3138</strong></a>: The <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></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.</li>
<li><span class="target" id="index-1148"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3120"><strong>PEP 3120</strong></a>: The default source encoding is now UTF-8.</li>
<li><span class="target" id="index-1149"></span><a class="reference external" href="http://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.)</li>
<li>The <tt class="xref docutils literal"><span class="pre">StringIO</span></tt> and <tt class="xref docutils literal"><span class="pre">cStringIO</span></tt> modules are gone.  Instead,
import the <a title="Core tools for working with streams." class="reference external" href="../library/io.html#module-io"><tt class="xref docutils literal"><span class="pre">io</span></tt></a> module and use <a title="io.StringIO" class="reference external" href="../library/io.html#io.StringIO"><tt class="xref docutils literal"><span class="pre">io.StringIO</span></tt></a> or
<a title="io.BytesIO" class="reference external" href="../library/io.html#io.BytesIO"><tt class="xref docutils literal"><span class="pre">io.BytesIO</span></tt></a> for text and data respectively.</li>
<li>See also the <a class="reference external" href="../howto/unicode.html#unicode-howto"><em>Unicode HOWTO</em></a>, which was updated for Python 3.0.</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 class="first"><span class="target" id="index-1150"></span><a class="reference external" href="http://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&#8217;s parameters
and return value.  There are no semantics attached to such
annotations except that they can be introspected at runtime using
the <tt class="xref docutils literal"><span class="pre">__annotations__</span></tt> attribute.  The intent is to encourage
experimentation through metaclasses, decorators or frameworks.</p>
</li>
<li><p class="first"><span class="target" id="index-1151"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3102"><strong>PEP 3102</strong></a>: Keyword-only arguments.  Named parameters occurring
after <tt class="docutils literal"><span class="pre">*args</span></tt> in the parameter list <em>must</em> be specified using
keyword syntax in the call.  You can also use a bare <tt class="docutils literal"><span class="pre">*</span></tt> in the
parameter list to indicate that you don&#8217;t accept a variable-length
argument list, but you do have keyword-only arguments.</p>
</li>
<li><p class="first">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 class="first"><span class="target" id="index-1152"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3104"><strong>PEP 3104</strong></a>: <a class="reference external" href="../reference/simple_stmts.html#nonlocal"><tt class="xref docutils literal"><span class="pre">nonlocal</span></tt></a> statement.  Using <tt class="docutils literal"><span class="pre">nonlocal</span> <span class="pre">x</span></tt>
you can now assign directly to a variable in an outer (but
non-global) scope.  <a class="reference external" href="../reference/simple_stmts.html#nonlocal"><tt class="xref docutils literal"><span class="pre">nonlocal</span></tt></a> is a new reserved word.</p>
</li>
<li><p class="first"><span class="target" id="index-1153"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3132"><strong>PEP 3132</strong></a>: Extended Iterable Unpacking.  You can now write things
like <tt class="docutils literal"><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></tt>.  And even <tt class="docutils literal"><span class="pre">*rest,</span> <span class="pre">a</span> <span class="pre">=</span>
<span class="pre">stuff</span></tt>.  The <tt class="docutils literal"><span class="pre">rest</span></tt> object is always a (possibly empty) list; the
right-hand side may be any iterable.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><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 <tt class="docutils literal"><span class="pre">0</span></tt>, <em>b</em> to <tt class="docutils literal"><span class="pre">4</span></tt>, and <em>rest</em> to <tt class="docutils literal"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></tt>.</p>
</li>
<li><p class="first">Dictionary comprehensions: <tt class="docutils literal"><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></tt> means the
same thing as <tt class="docutils literal"><span class="pre">dict(stuff)</span></tt> but is more flexible.  (This is
<span class="target" id="index-1154"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0274"><strong>PEP 0274</strong></a> vindicated. :-)</p>
</li>
<li><p class="first">Set literals, e.g. <tt class="docutils literal"><span class="pre">{1,</span> <span class="pre">2}</span></tt>.  Note that <tt class="docutils literal"><span class="pre">{}</span></tt> is an empty
dictionary; use <tt class="docutils literal"><span class="pre">set()</span></tt> for an empty set.  Set comprehensions are
also supported; e.g., <tt class="docutils literal"><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></tt> means the same thing as
<tt class="docutils literal"><span class="pre">set(stuff)</span></tt> but is more flexible.</p>
</li>
<li><p class="first">New octal literals, e.g. <tt class="docutils literal"><span class="pre">0o720</span></tt> (already in 2.6).  The old octal
literals (<tt class="docutils literal"><span class="pre">0720</span></tt>) are gone.</p>
</li>
<li><p class="first">New binary literals, e.g. <tt class="docutils literal"><span class="pre">0b1010</span></tt> (already in 2.6), and
there is a new corresponding builtin function, <a title="bin" class="reference external" href="../library/functions.html#bin"><tt class="xref docutils literal"><span class="pre">bin()</span></tt></a>.</p>
</li>
<li><p class="first">Bytes literals are introduced with a leading <tt class="docutils literal"><span class="pre">b</span></tt> or <tt class="docutils literal"><span class="pre">B</span></tt>, and
there is a new corresponding builtin function, <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes()</span></tt></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 class="first"><span class="target" id="index-1155"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a> and <span class="target" id="index-1156"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: new <a class="reference external" href="../reference/simple_stmts.html#raise"><tt class="xref docutils literal"><span class="pre">raise</span></tt></a> statement syntax:
<tt class="docutils literal"><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></tt>.  See below.</p>
</li>
<li><p class="first"><a class="reference external" href="../reference/compound_stmts.html#as"><tt class="xref docutils literal"><span class="pre">as</span></tt></a> and <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> are now reserved words.  (Since
2.6, actually.)</p>
</li>
<li><p class="first"><tt class="xref xref docutils literal"><span class="pre">True</span></tt>, <tt class="xref xref docutils literal"><span class="pre">False</span></tt>, and <tt class="xref xref docutils literal"><span class="pre">None</span></tt> are reserved
words.  (2.6 partially enforced the restrictions on <tt class="xref xref docutils literal"><span class="pre">None</span></tt>
already.)</p>
</li>
<li><p class="first">Change from <a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> <em>exc</em>, <em>var</em> to
<a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> <em>exc</em> <a class="reference external" href="../reference/compound_stmts.html#as"><tt class="xref docutils literal"><span class="pre">as</span></tt></a> <em>var</em>.  See <span class="target" id="index-1157"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>.</p>
</li>
<li><p class="first"><span class="target" id="index-1158"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a>: New Metaclass Syntax.  Instead of:</p>
<div class="highlight-python3"><pre>class C:
    __metaclass__ = M
    ...</pre>
</div>
<p>you must now use:</p>
<div class="highlight-python3"><div class="highlight"><pre><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 <tt class="xref docutils literal"><span class="pre">__metaclass__</span></tt> 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 title="object" class="reference external" href="../library/functions.html#object"><tt class="xref docutils literal"><span class="pre">object</span></tt></a>.)</p>
</li>
<li><p class="first">List comprehensions no longer support the syntactic form
<tt class="docutils literal"><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></tt>.  Use
<tt class="docutils literal"><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></tt> instead.
Also note that list comprehensions have different semantics: they
are closer to syntactic sugar for a generator expression inside a
<a title="list" class="reference external" href="../library/functions.html#list"><tt class="xref docutils literal"><span class="pre">list()</span></tt></a> constructor, and in particular the loop control
variables are no longer leaked into the surrounding scope.</p>
</li>
<li><p class="first">The <em>ellipsis</em> (<tt class="docutils literal"><span class="pre">...</span></tt>) can be used as an atomic expression
anywhere.  (Previously it was only allowed in slices.)  Also, it
<em>must</em> now be spelled as <tt class="docutils literal"><span class="pre">...</span></tt>.  (Previously it could also be
spelled as <tt class="docutils literal"><span class="pre">.</span> <span class="pre">.</span> <span class="pre">.</span></tt>, 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><span class="target" id="index-1159"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3113"><strong>PEP 3113</strong></a>: Tuple parameter unpacking removed.  You can no longer
write <tt class="docutils literal"><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></tt>.
Use <tt class="docutils literal"><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></tt> instead.</li>
<li>Removed backticks (use <a title="repr" class="reference external" href="../library/functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> instead).</li>
<li>Removed <tt class="docutils literal"><span class="pre">&lt;&gt;</span></tt> (use <tt class="docutils literal"><span class="pre">!=</span></tt> instead).</li>
<li>Removed keyword: <a title="exec" class="reference external" href="../library/functions.html#exec"><tt class="xref docutils literal"><span class="pre">exec()</span></tt></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 title="exec" class="reference external" href="../library/functions.html#exec"><tt class="xref docutils literal"><span class="pre">exec()</span></tt></a> no longer takes a stream argument;
instead of <tt class="docutils literal"><span class="pre">exec(f)</span></tt> you can use <tt class="docutils literal"><span class="pre">exec(f.read())</span></tt>.</li>
<li>Integer literals no longer support a trailing <tt class="docutils literal"><span class="pre">l</span></tt> or <tt class="docutils literal"><span class="pre">L</span></tt>.</li>
<li>String literals no longer support a leading <tt class="docutils literal"><span class="pre">u</span></tt> or <tt class="docutils literal"><span class="pre">U</span></tt>.</li>
<li>The <a class="reference external" href="../reference/simple_stmts.html#from"><tt class="xref docutils literal"><span class="pre">from</span></tt></a> <em>module</em> <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> <tt class="docutils literal"><span class="pre">*</span></tt> syntax is only
allowed at the module level, no longer inside functions.</li>
<li>The only acceptable syntax for relative imports is <tt class="docutils literal"><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></tt>.  All <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> forms not starting with <tt class="docutils literal"><span class="pre">.</span></tt> are
interpreted as absolute imports.  (<span class="target" id="index-1160"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0328"><strong>PEP 0328</strong></a>)</li>
<li>Classic classes are gone.</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 external" href="2.6.html#whats-new-in-2-6"><em>What&#8217;s New in Python 2.6</em></a> should be
consulted for longer descriptions.</p>
<ul class="simple">
<li><a class="reference external" href="2.6.html#pep-0343"><em>PEP 343: The &#8216;with&#8217; statement</em></a>.  The <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement is now a standard
feature and no longer needs to be imported from the <a title="Future statement definitions" class="reference external" href="../library/__future__.html#module-__future__"><tt class="xref docutils literal"><span class="pre">__future__</span></tt></a>.
Also check out <a class="reference external" href="2.6.html#new-26-context-managers"><em>Writing Context Managers</em></a> and
<a class="reference external" href="2.6.html#new-module-contextlib"><em>The contextlib module</em></a>.</li>
<li><a class="reference external" href="2.6.html#pep-0366"><em>PEP 366: Explicit Relative Imports From a Main Module</em></a>.  This enhances the usefulness of the <a class="reference external" href="../using/cmdline.html#cmdoption-m"><em class="xref">-m</em></a>
option when the referenced module lives in a package.</li>
<li><a class="reference external" href="2.6.html#pep-0370"><em>PEP 370: Per-user site-packages Directory</em></a>.</li>
<li><a class="reference external" href="2.6.html#pep-0371"><em>PEP 371: The multiprocessing Package</em></a>.</li>
<li><a class="reference external" href="2.6.html#pep-3101"><em>PEP 3101: Advanced String Formatting</em></a>.  Note: the 2.6 description mentions the
<a title="format" class="reference external" href="../library/functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> method for both 8-bit and Unicode strings.  In 3.0,
only the <a title="str" class="reference external" href="../library/functions.html#str"><tt class="xref docutils literal"><span class="pre">str</span></tt></a> type (text strings with Unicode support)
supports this method; the <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a> type does not.  The plan is
to eventually make this the only API for string formatting, and to
start deprecating the <tt class="docutils literal"><span class="pre">%</span></tt> operator in Python 3.1.</li>
<li><a class="reference external" href="2.6.html#pep-3105"><em>PEP 3105: print As a Function</em></a>.  This is now a standard feature and no longer needs
to be imported from <a title="Future statement definitions" class="reference external" href="../library/__future__.html#module-__future__"><tt class="xref docutils literal"><span class="pre">__future__</span></tt></a>.  More details were given above.</li>
<li><a class="reference external" href="2.6.html#pep-3110"><em>PEP 3110: Exception-Handling Changes</em></a>.  The <a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> <em>exc</em> <a class="reference external" href="../reference/compound_stmts.html#as"><tt class="xref docutils literal"><span class="pre">as</span></tt></a> <em>var</em>
syntax is now standard and <a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> <em>exc</em>, <em>var</em> is no
longer supported.  (Of course, the <a class="reference external" href="../reference/compound_stmts.html#as"><tt class="xref docutils literal"><span class="pre">as</span></tt></a> <em>var</em> part is still
optional.)</li>
<li><a class="reference external" href="2.6.html#pep-3112"><em>PEP 3112: Byte Literals</em></a>.  The <tt class="docutils literal"><span class="pre">b&quot;...&quot;</span></tt> string literal notation (and its
variants like <tt class="docutils literal"><span class="pre">b'...'</span></tt>, <tt class="docutils literal"><span class="pre">b&quot;&quot;&quot;...&quot;&quot;&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">br&quot;...&quot;</span></tt>) now
produces a literal of type <a title="bytes" class="reference external" href="../library/functions.html#bytes"><tt class="xref docutils literal"><span class="pre">bytes</span></tt></a>.</li>
<li><a class="reference external" href="2.6.html#pep-3116"><em>PEP 3116: New I/O Library</em></a>.  The <a title="Core tools for working with streams." class="reference external" href="../library/io.html#module-io"><tt class="xref docutils literal"><span class="pre">io</span></tt></a> module is now the standard way of
doing file I/O, and the initial values of <a title="sys.stdin" class="reference external" href="../library/sys.html#sys.stdin"><tt class="xref docutils literal"><span class="pre">sys.stdin</span></tt></a>,
<a title="sys.stdout" class="reference external" href="../library/sys.html#sys.stdout"><tt class="xref docutils literal"><span class="pre">sys.stdout</span></tt></a> and <a title="sys.stderr" class="reference external" href="../library/sys.html#sys.stderr"><tt class="xref docutils literal"><span class="pre">sys.stderr</span></tt></a> are now instances of
<a title="io.TextIOBase" class="reference external" href="../library/io.html#io.TextIOBase"><tt class="xref docutils literal"><span class="pre">io.TextIOBase</span></tt></a>.  The builtin <a title="open" class="reference external" href="../library/functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a> function is now an
alias for <a title="io.open" class="reference external" href="../library/io.html#io.open"><tt class="xref docutils literal"><span class="pre">io.open()</span></tt></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 title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>, not
<a title="exceptions.IOError" class="reference external" href="../library/exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a>.  The binary file object underlying a text file
object can be accessed as <tt class="xref docutils literal"><span class="pre">f.buffer</span></tt> (but beware that the
text object maintains a buffer of itself in order to speed up
the encoding and decoding operations).</li>
<li><a class="reference external" href="2.6.html#pep-3118"><em>PEP 3118: Revised Buffer Protocol</em></a>.  The old builtin <tt class="xref docutils literal"><span class="pre">buffer()</span></tt> is now really gone;
the new builtin <a title="memoryview" class="reference external" href="../library/stdtypes.html#memoryview"><tt class="xref docutils literal"><span class="pre">memoryview()</span></tt></a> provides (mostly) similar
functionality.</li>
<li><a class="reference external" href="2.6.html#pep-3119"><em>PEP 3119: Abstract Base Classes</em></a>.  The <a title="Abstract base classes according to PEP 3119." class="reference external" href="../library/abc.html#module-abc"><tt class="xref docutils literal"><span class="pre">abc</span></tt></a> module and the ABCs defined in the
<a title="Container datatypes" class="reference external" href="../library/collections.html#module-collections"><tt class="xref docutils literal"><span class="pre">collections</span></tt></a> module plays a somewhat more prominent role in
the language now, and builtin collection types like <a title="dict" class="reference external" href="../library/stdtypes.html#dict"><tt class="xref docutils literal"><span class="pre">dict</span></tt></a>
and <a title="list" class="reference external" href="../library/functions.html#list"><tt class="xref docutils literal"><span class="pre">list</span></tt></a> conform to the <tt class="xref docutils literal"><span class="pre">collections.MutableMapping</span></tt>
and <tt class="xref docutils literal"><span class="pre">collections.MutableSequence</span></tt> ABCs, respectively.</li>
<li><a class="reference external" href="2.6.html#pep-3127"><em>PEP 3127: Integer Literal Support and Syntax</em></a>.  As mentioned above, the new octal literal
notation is the only one supported, and binary literals have been
added.</li>
<li><a class="reference external" href="2.6.html#pep-3129"><em>PEP 3129: Class Decorators</em></a>.</li>
<li><a class="reference external" href="2.6.html#pep-3141"><em>PEP 3141: A Type Hierarchy for Numbers</em></a>.  The <a title="Numeric abstract base classes (Complex, Real, Integral, etc.)." class="reference external" href="../library/numbers.html#module-numbers"><tt class="xref docutils literal"><span class="pre">numbers</span></tt></a> module is another new use of
ABCs, defining Python&#8217;s &#8220;numeric tower&#8221;.  Also note the new
<a title="Rational numbers." class="reference external" href="../library/fractions.html#module-fractions"><tt class="xref docutils literal"><span class="pre">fractions</span></tt></a> module which implements <a title="numbers.Rational" class="reference external" href="../library/numbers.html#numbers.Rational"><tt class="xref docutils literal"><span class="pre">numbers.Rational</span></tt></a>.</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-1161"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> is the
reference for the major changes to the library.  Here&#8217;s a capsule
review:</p>
<ul>
<li><p class="first">Many old modules were removed.  Some, like <tt class="xref docutils literal"><span class="pre">gopherlib</span></tt> (no
longer used) and <tt class="xref docutils literal"><span class="pre">md5</span></tt> (replaced by <a title="Secure hash and message digest algorithms." class="reference external" href="../library/hashlib.html#module-hashlib"><tt class="xref docutils literal"><span class="pre">hashlib</span></tt></a>), were
already deprecated by <span class="target" id="index-1162"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0004"><strong>PEP 0004</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-1163"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0011"><strong>PEP 0011</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-1164"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a> for an exhaustive list.</p>
</li>
<li><p class="first">The <tt class="xref docutils literal"><span class="pre">bsddb3</span></tt> 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&#8217;s
release schedule.  However, the package is alive and well,
externally maintained at <a class="reference external" href="http://www.jcea.es/programacion/pybsddb.htm">http://www.jcea.es/programacion/pybsddb.htm</a>.</p>
</li>
<li><p class="first">Some modules were renamed because their old name disobeyed
<span class="target" id="index-1165"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0008"><strong>PEP 0008</strong></a>, or for various other reasons.  Here&#8217;s the list:</p>
<table border="1" class="docutils">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head"><p class="first last">Old Name</p>
</th>
<th class="head"><p class="first last">New Name</p>
</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><p class="first last">_winreg</p>
</td>
<td><p class="first last">winreg</p>
</td>
</tr>
<tr><td><p class="first last">ConfigParser</p>
</td>
<td><p class="first last">configparser</p>
</td>
</tr>
<tr><td><p class="first last">copy_reg</p>
</td>
<td><p class="first last">copyreg</p>
</td>
</tr>
<tr><td><p class="first last">Queue</p>
</td>
<td><p class="first last">queue</p>
</td>
</tr>
<tr><td><p class="first last">SocketServer</p>
</td>
<td><p class="first last">socketserver</p>
</td>
</tr>
<tr><td><p class="first last">markupbase</p>
</td>
<td><p class="first last">_markupbase</p>
</td>
</tr>
<tr><td><p class="first last">repr</p>
</td>
<td><p class="first last">reprlib</p>
</td>
</tr>
<tr><td><p class="first last">test.test_support</p>
</td>
<td><p class="first last">test.support</p>
</td>
</tr>
</tbody>
</table>
</li>
<li><p class="first">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 title="Convert Python objects to streams of bytes and back." class="reference external" href="../library/pickle.html#module-pickle"><tt class="xref docutils literal"><span class="pre">pickle</span></tt></a> and
<tt class="xref docutils literal"><span class="pre">cPickle</span></tt>.  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 title="Convert Python objects to streams of bytes and back." class="reference external" href="../library/pickle.html#module-pickle"><tt class="xref docutils literal"><span class="pre">pickle</span></tt></a> / <tt class="xref docutils literal"><span class="pre">cPickle</span></tt> pair received this
treatment.  The <tt class="xref docutils literal"><span class="pre">profile</span></tt> module is on the list for 3.1.  The
<tt class="xref docutils literal"><span class="pre">StringIO</span></tt> module has been turned into a class in the <a title="Core tools for working with streams." class="reference external" href="../library/io.html#module-io"><tt class="xref docutils literal"><span class="pre">io</span></tt></a>
module.</p>
</li>
<li><p class="first">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><a title="Interfaces to various Unix &quot;database&quot; formats." class="reference external" href="../library/dbm.html#module-dbm"><tt class="xref docutils literal"><span class="pre">dbm</span></tt></a> (<tt class="xref docutils literal"><span class="pre">anydbm</span></tt>, <tt class="xref docutils literal"><span class="pre">dbhash</span></tt>, <a title="Interfaces to various Unix &quot;database&quot; formats." class="reference external" href="../library/dbm.html#module-dbm"><tt class="xref docutils literal"><span class="pre">dbm</span></tt></a>,
<tt class="xref docutils literal"><span class="pre">dumbdbm</span></tt>, <tt class="xref docutils literal"><span class="pre">gdbm</span></tt>, <tt class="xref docutils literal"><span class="pre">whichdb</span></tt>).</li>
<li><tt class="xref docutils literal"><span class="pre">html</span></tt> (<tt class="xref docutils literal"><span class="pre">HTMLParser</span></tt>, <tt class="xref docutils literal"><span class="pre">htmlentitydefs</span></tt>).</li>
<li><tt class="xref docutils literal"><span class="pre">http</span></tt> (<tt class="xref docutils literal"><span class="pre">httplib</span></tt>, <tt class="xref docutils literal"><span class="pre">BaseHTTPServer</span></tt>,
<tt class="xref docutils literal"><span class="pre">CGIHTTPServer</span></tt>, <tt class="xref docutils literal"><span class="pre">SimpleHTTPServer</span></tt>, <tt class="xref docutils literal"><span class="pre">Cookie</span></tt>,
<tt class="xref docutils literal"><span class="pre">cookielib</span></tt>).</li>
<li><a title="Interface to Tcl/Tk for graphical user interfaces" class="reference external" href="../library/tkinter.html#module-tkinter"><tt class="xref docutils literal"><span class="pre">tkinter</span></tt></a> (all <tt class="xref docutils literal"><span class="pre">Tkinter</span></tt>-related modules except
<a title="Turtle graphics for Tk" class="reference external" href="../library/turtle.html#module-turtle"><tt class="xref docutils literal"><span class="pre">turtle</span></tt></a>).  The target audience of <a title="Turtle graphics for Tk" class="reference external" href="../library/turtle.html#module-turtle"><tt class="xref docutils literal"><span class="pre">turtle</span></tt></a> doesn&#8217;t
really care about <a title="Interface to Tcl/Tk for graphical user interfaces" class="reference external" href="../library/tkinter.html#module-tkinter"><tt class="xref docutils literal"><span class="pre">tkinter</span></tt></a>.  Also note that as of Python
2.6, the functionality of <a title="Turtle graphics for Tk" class="reference external" href="../library/turtle.html#module-turtle"><tt class="xref docutils literal"><span class="pre">turtle</span></tt></a> has been greatly enhanced.</li>
<li><tt class="xref docutils literal"><span class="pre">urllib</span></tt> (<tt class="xref docutils literal"><span class="pre">urllib</span></tt>, <tt class="xref docutils literal"><span class="pre">urllib2</span></tt>, <tt class="xref docutils literal"><span class="pre">urlparse</span></tt>,
<tt class="xref docutils literal"><span class="pre">robotparse</span></tt>).</li>
<li><tt class="xref docutils literal"><span class="pre">xmlrpc</span></tt> (<tt class="xref docutils literal"><span class="pre">xmlrpclib</span></tt>, <tt class="xref docutils literal"><span class="pre">DocXMLRPCServer</span></tt>,
<tt class="xref docutils literal"><span class="pre">SimpleXMLRPCServer</span></tt>).</li>
</ul>
</li>
</ul>
<p>Some other changes to standard library modules, not covered by
<span class="target" id="index-1166"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3108"><strong>PEP 3108</strong></a>:</p>
<ul class="simple">
<li>Killed <tt class="xref docutils literal"><span class="pre">sets</span></tt>.  Use the builtin <a title="set" class="reference external" href="../library/stdtypes.html#set"><tt class="xref docutils literal"><span class="pre">set()</span></tt></a> function.</li>
<li>Cleanup of the <a title="Access system-specific parameters and functions." class="reference external" href="../library/sys.html#module-sys"><tt class="xref docutils literal"><span class="pre">sys</span></tt></a> module: removed <tt class="xref docutils literal"><span class="pre">sys.exitfunc()</span></tt>,
<tt class="xref docutils literal"><span class="pre">sys.exc_clear()</span></tt>, <tt class="xref docutils literal"><span class="pre">sys.exc_type</span></tt>, <tt class="xref docutils literal"><span class="pre">sys.exc_value</span></tt>,
<tt class="xref docutils literal"><span class="pre">sys.exc_traceback</span></tt>.  (Note that <a title="sys.last_type" class="reference external" href="../library/sys.html#sys.last_type"><tt class="xref docutils literal"><span class="pre">sys.last_type</span></tt></a>
etc. remain.)</li>
<li>Cleanup of the <a title="array.array" class="reference external" href="../library/array.html#array.array"><tt class="xref docutils literal"><span class="pre">array.array</span></tt></a> type: the <tt class="xref docutils literal"><span class="pre">read()</span></tt> and
<tt class="xref docutils literal"><span class="pre">write()</span></tt> methods are gone; use <tt class="xref docutils literal"><span class="pre">fromfile()</span></tt> and
<tt class="xref docutils literal"><span class="pre">tofile()</span></tt> instead.  Also, the <tt class="docutils literal"><span class="pre">'c'</span></tt> typecode for array is
gone &#8211; use either <tt class="docutils literal"><span class="pre">'b'</span></tt> for bytes or <tt class="docutils literal"><span class="pre">'u'</span></tt> for Unicode
characters.</li>
<li>Cleanup of the <a title="Functions corresponding to the standard operators." class="reference external" href="../library/operator.html#module-operator"><tt class="xref docutils literal"><span class="pre">operator</span></tt></a> module: removed
<tt class="xref docutils literal"><span class="pre">sequenceIncludes()</span></tt> and <tt class="xref docutils literal"><span class="pre">isCallable()</span></tt>.</li>
<li>Cleanup of the <tt class="xref docutils literal"><span class="pre">thread</span></tt> module: <tt class="xref docutils literal"><span class="pre">acquire_lock()</span></tt> and
<tt class="xref docutils literal"><span class="pre">release_lock()</span></tt> are gone; use <tt class="xref docutils literal"><span class="pre">acquire()</span></tt> and
<tt class="xref docutils literal"><span class="pre">release()</span></tt> instead.</li>
<li>Cleanup of the <a title="Generate pseudo-random numbers with various common distributions." class="reference external" href="../library/random.html#module-random"><tt class="xref docutils literal"><span class="pre">random</span></tt></a> module: removed the <tt class="xref docutils literal"><span class="pre">jumpahead()</span></tt> API.</li>
<li>The <tt class="xref docutils literal"><span class="pre">new</span></tt> module is gone.</li>
<li>The functions <tt class="xref docutils literal"><span class="pre">os.tmpnam()</span></tt>, <tt class="xref docutils literal"><span class="pre">os.tempnam()</span></tt> and
<tt class="xref docutils literal"><span class="pre">os.tmpfile()</span></tt> have been removed in favor of the <a title="Generate temporary files and directories." class="reference external" href="../library/tempfile.html#module-tempfile"><tt class="xref docutils literal"><span class="pre">tempfile</span></tt></a>
module.</li>
<li>The <a title="Lexical scanner for Python source code." class="reference external" href="../library/tokenize.html#module-tokenize"><tt class="xref docutils literal"><span class="pre">tokenize</span></tt></a> module has been changed to work with bytes.  The
main entry point is now <a title="tokenize.tokenize" class="reference external" href="../library/tokenize.html#tokenize.tokenize"><tt class="xref docutils literal"><span class="pre">tokenize.tokenize()</span></tt></a>, instead of
generate_tokens.</li>
<li><tt class="xref docutils literal"><span class="pre">string.letters</span></tt> and its friends (<tt class="xref docutils literal"><span class="pre">string.lowercase</span></tt> and
<tt class="xref docutils literal"><span class="pre">string.uppercase</span></tt>) are gone.  Use
<a title="string.ascii_letters" class="reference external" href="../library/string.html#string.ascii_letters"><tt class="xref docutils literal"><span class="pre">string.ascii_letters</span></tt></a> etc. instead.  (The reason for the
removal is that <tt class="xref docutils literal"><span class="pre">string.letters</span></tt> and friends had
locale-specific behavior, which is a bad idea for such
attractively-named global &#8220;constants&#8221;.)</li>
<li>Renamed module <tt class="xref docutils literal"><span class="pre">__builtin__</span></tt> to <a title="The module that provides the built-in namespace." class="reference external" href="../library/builtins.html#module-builtins"><tt class="xref docutils literal"><span class="pre">builtins</span></tt></a> (removing the
underscores, adding an &#8216;s&#8217;).  The <tt class="xref docutils literal"><span class="pre">__builtins__</span></tt> variable
found in most global namespaces is unchanged.  To modify a builtin,
you should use <a title="The module that provides the built-in namespace." class="reference external" href="../library/builtins.html#module-builtins"><tt class="xref docutils literal"><span class="pre">builtins</span></tt></a>, not <tt class="xref docutils literal"><span class="pre">__builtins__</span></tt>!</li>
</ul>
</div>
<div class="section" id="pep-3101-a-new-approach-to-string-formatting">
<h2><span class="target" id="index-1167"></span><a class="reference external" href="http://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>A new system for  built-in string formatting operations replaces the
<tt class="docutils literal"><span class="pre">%</span></tt> string  formatting operator.   (However, the <tt class="docutils literal"><span class="pre">%</span></tt>  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-1168"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a> for the full
scoop.</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 class="first"><span class="target" id="index-1169"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0352"><strong>PEP 0352</strong></a>: All exceptions must be derived (directly or indirectly)
from <a title="exceptions.BaseException" class="reference external" href="../library/exceptions.html#exceptions.BaseException"><tt class="xref docutils literal"><span class="pre">BaseException</span></tt></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 title="exceptions.BaseException" class="reference external" href="../library/exceptions.html#exceptions.BaseException"><tt class="xref docutils literal"><span class="pre">BaseException</span></tt></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 class="first">Almost all exceptions should actually derive from <a title="exceptions.Exception" class="reference external" href="../library/exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">Exception</span></tt></a>;
<a title="exceptions.BaseException" class="reference external" href="../library/exceptions.html#exceptions.BaseException"><tt class="xref docutils literal"><span class="pre">BaseException</span></tt></a> should only be used as a base class for
exceptions that should only be handled at the top level, such as
<a title="exceptions.SystemExit" class="reference external" href="../library/exceptions.html#exceptions.SystemExit"><tt class="xref docutils literal"><span class="pre">SystemExit</span></tt></a> or <a title="exceptions.KeyboardInterrupt" class="reference external" href="../library/exceptions.html#exceptions.KeyboardInterrupt"><tt class="xref docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a>.  The recommended
idiom for handling all exceptions except for this latter category is
to use <a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> <a title="exceptions.Exception" class="reference external" href="../library/exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">Exception</span></tt></a>.</p>
</li>
<li><p class="first"><tt class="xref docutils literal"><span class="pre">StandardError</span></tt> was removed.</p>
</li>
<li><p class="first">Exceptions no longer behave as sequences.  Use the <tt class="xref docutils literal"><span class="pre">args</span></tt>
attribute instead.</p>
</li>
<li><p class="first"><span class="target" id="index-1170"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3109"><strong>PEP 3109</strong></a>: Raising exceptions.  You must now use <tt class="docutils literal"><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></tt> instead of <tt class="docutils literal"><span class="pre">raise</span> <em><span class="pre">Exception</span></em><span class="pre">,</span> <em><span class="pre">args</span></em></tt>.
Additionally, you can no longer explicitly specify a traceback;
instead, if you <em>have</em> to do this, you can assign directly to the
<tt class="xref docutils literal"><span class="pre">__traceback__</span></tt> attribute (see below).</p>
</li>
<li><p class="first"><span class="target" id="index-1171"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a>: Catching exceptions.  You must now use
<tt class="docutils literal"><span class="pre">except</span> <em><span class="pre">SomeException</span></em> <span class="pre">as</span> <em><span class="pre">variable</span></em></tt> instead
of <tt class="docutils literal"><span class="pre">except</span> <em><span class="pre">SomeException</span></em><span class="pre">,</span> <em><span class="pre">variable</span></em></tt>.  Moreover, the
<em>variable</em> is explicitly deleted when the <a class="reference external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> block
is left.</p>
</li>
<li><p class="first"><span class="target" id="index-1172"></span><a class="reference external" href="http://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 external" href="../reference/compound_stmts.html#except"><tt class="xref docutils literal"><span class="pre">except</span></tt></a> or <a class="reference external" href="../reference/compound_stmts.html#finally"><tt class="xref docutils literal"><span class="pre">finally</span></tt></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
<tt class="xref docutils literal"><span class="pre">__context__</span></tt> attribute of the secondary exception.
Explicit chaining is invoked with this syntax:</p>
<div class="highlight-python3"><div class="highlight"><pre><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
<tt class="xref docutils literal"><span class="pre">__cause__</span></tt> attribute of the secondary exception.  The
traceback printed when an unhandled exception occurs walks the chain
of <tt class="xref docutils literal"><span class="pre">__cause__</span></tt> and <tt class="xref docutils literal"><span class="pre">__context__</span></tt> 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 class="first"><span class="target" id="index-1173"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3134"><strong>PEP 3134</strong></a>: Exception objects now store their traceback as the
<tt class="xref docutils literal"><span class="pre">__traceback__</span></tt> attribute.  This means that an exception
object now contains all the information pertaining to an exception,
and there are fewer reasons to use <a title="sys.exc_info" class="reference external" href="../library/sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a> (though the
latter is not removed).</p>
</li>
<li><p class="first">A few exception messages are improved when Windows fails to load an
extension module.  For example, <tt class="docutils literal"><span class="pre">error</span> <span class="pre">code</span> <span class="pre">193</span></tt> is now <tt class="docutils literal"><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></tt>.  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><tt class="docutils literal"><span class="pre">!=</span></tt> now returns the opposite of <tt class="docutils literal"><span class="pre">==</span></tt>, unless <tt class="docutils literal"><span class="pre">==</span></tt> returns
<a title="NotImplemented" class="reference external" href="../library/constants.html#NotImplemented"><tt class="xref docutils literal"><span class="pre">NotImplemented</span></tt></a>.</li>
<li>The concept of &#8220;unbound methods&#8221; has been removed from the language.
When referencing a method as a class attribute, you now get a plain
function object.</li>
<li><tt class="xref docutils literal"><span class="pre">__getslice__()</span></tt>, <tt class="xref docutils literal"><span class="pre">__setslice__()</span></tt> and <tt class="xref docutils literal"><span class="pre">__delslice__()</span></tt>
were killed.  The syntax <tt class="docutils literal"><span class="pre">a[i:j]</span></tt> now translates to
<tt class="docutils literal"><span class="pre">a.__getitem__(slice(i,</span> <span class="pre">j))</span></tt> (or <a title="object.__setitem__" class="reference external" href="../reference/datamodel.html#object.__setitem__"><tt class="xref docutils literal"><span class="pre">__setitem__()</span></tt></a> or
<a title="object.__delitem__" class="reference external" href="../reference/datamodel.html#object.__delitem__"><tt class="xref docutils literal"><span class="pre">__delitem__()</span></tt></a>, when used as an assignment or deletion target,
respectively).</li>
<li><span class="target" id="index-1174"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3114"><strong>PEP 3114</strong></a>: the standard <a title="next" class="reference external" href="../library/functions.html#next"><tt class="xref docutils literal"><span class="pre">next()</span></tt></a> method has been renamed to
<tt class="xref docutils literal"><span class="pre">__next__()</span></tt>.</li>
<li>The <tt class="xref docutils literal"><span class="pre">__oct__()</span></tt> and <tt class="xref docutils literal"><span class="pre">__hex__()</span></tt> special methods are removed
&#8211; <a title="oct" class="reference external" href="../library/functions.html#oct"><tt class="xref docutils literal"><span class="pre">oct()</span></tt></a> and <a title="hex" class="reference external" href="../library/functions.html#hex"><tt class="xref docutils literal"><span class="pre">hex()</span></tt></a> use <a title="object.__index__" class="reference external" href="../reference/datamodel.html#object.__index__"><tt class="xref docutils literal"><span class="pre">__index__()</span></tt></a> now to convert
the argument to an integer.</li>
<li>Removed support for <tt class="xref docutils literal"><span class="pre">__members__</span></tt> and <tt class="xref docutils literal"><span class="pre">__methods__</span></tt>.</li>
<li>The function attributes named <tt class="xref docutils literal"><span class="pre">func_X</span></tt> have been renamed to
use the <tt class="xref docutils literal"><span class="pre">__X__</span></tt> form, freeing up these names in the function
attribute namespace for user-defined attributes.  To wit,
<tt class="xref docutils literal"><span class="pre">func_closure</span></tt>, <tt class="xref docutils literal"><span class="pre">func_code</span></tt>, <tt class="xref docutils literal"><span class="pre">func_defaults</span></tt>,
<tt class="xref docutils literal"><span class="pre">func_dict</span></tt>, <tt class="xref docutils literal"><span class="pre">func_doc</span></tt>, <tt class="xref docutils literal"><span class="pre">func_globals</span></tt>,
<tt class="xref docutils literal"><span class="pre">func_name</span></tt> were renamed to <tt class="xref docutils literal"><span class="pre">__closure__</span></tt>,
<tt class="xref docutils literal"><span class="pre">__code__</span></tt>, <tt class="xref docutils literal"><span class="pre">__defaults__</span></tt>, <tt class="xref docutils literal"><span class="pre">__dict__</span></tt>,
<tt class="xref docutils literal"><span class="pre">__doc__</span></tt>, <tt class="xref docutils literal"><span class="pre">__globals__</span></tt>, <tt class="xref docutils literal"><span class="pre">__name__</span></tt>,
respectively.</li>
<li><tt class="xref docutils literal"><span class="pre">__nonzero__()</span></tt> is now <a title="object.__bool__" class="reference external" href="../reference/datamodel.html#object.__bool__"><tt class="xref docutils literal"><span class="pre">__bool__()</span></tt></a>.</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><span class="target" id="index-1175"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3135"><strong>PEP 3135</strong></a>: New <a title="super" class="reference external" href="../library/functions.html#super"><tt class="xref docutils literal"><span class="pre">super()</span></tt></a>.  You can now invoke <a title="super" class="reference external" href="../library/functions.html#super"><tt class="xref docutils literal"><span class="pre">super()</span></tt></a>
without arguments and (assuming this is in a regular instance method
defined inside a <a class="reference external" href="../reference/compound_stmts.html#class"><tt class="xref docutils literal"><span class="pre">class</span></tt></a> statement) the right class and
instance will automatically be chosen.  With arguments, the behavior
of <a title="super" class="reference external" href="../library/functions.html#super"><tt class="xref docutils literal"><span class="pre">super()</span></tt></a> is unchanged.</li>
<li><span class="target" id="index-1176"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3111"><strong>PEP 3111</strong></a>: <tt class="xref docutils literal"><span class="pre">raw_input()</span></tt> was renamed to <a title="input" class="reference external" href="../library/functions.html#input"><tt class="xref docutils literal"><span class="pre">input()</span></tt></a>.  That
is, the new <a title="input" class="reference external" href="../library/functions.html#input"><tt class="xref docutils literal"><span class="pre">input()</span></tt></a> function reads a line from
<a title="sys.stdin" class="reference external" href="../library/sys.html#sys.stdin"><tt class="xref docutils literal"><span class="pre">sys.stdin</span></tt></a> and returns it with the trailing newline stripped.
It raises <a title="exceptions.EOFError" class="reference external" href="../library/exceptions.html#exceptions.EOFError"><tt class="xref docutils literal"><span class="pre">EOFError</span></tt></a> if the input is terminated prematurely.
To get the old behavior of <a title="input" class="reference external" href="../library/functions.html#input"><tt class="xref docutils literal"><span class="pre">input()</span></tt></a>, use <tt class="docutils literal"><span class="pre">eval(input())</span></tt>.</li>
<li>A new builtin <a title="next" class="reference external" href="../library/functions.html#next"><tt class="xref docutils literal"><span class="pre">next()</span></tt></a> was added to call the <tt class="xref docutils literal"><span class="pre">__next__()</span></tt>
method on an object.</li>
<li>Moved <tt class="xref docutils literal"><span class="pre">intern()</span></tt> to <a title="sys.intern" class="reference external" href="../library/sys.html#sys.intern"><tt class="xref docutils literal"><span class="pre">sys.intern()</span></tt></a>.</li>
<li>Removed: <tt class="xref docutils literal"><span class="pre">apply()</span></tt>.  Instead of <tt class="docutils literal"><span class="pre">apply(f,</span> <span class="pre">args)</span></tt> use
<tt class="docutils literal"><span class="pre">f(*args)</span></tt>.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">callable()</span></tt>.  Instead of <tt class="docutils literal"><span class="pre">callable(f)</span></tt> you can use
<tt class="docutils literal"><span class="pre">hasattr(f,</span> <span class="pre">'__call__')</span></tt>.  The <tt class="xref docutils literal"><span class="pre">operator.isCallable()</span></tt> function
is also gone.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">coerce()</span></tt>.  This function no longer serves a purpose
now that classic classes are gone.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">execfile()</span></tt>.  Instead of <tt class="docutils literal"><span class="pre">execfile(fn)</span></tt> use
<tt class="docutils literal"><span class="pre">exec(open(fn).read())</span></tt>.</li>
<li>Removed the <tt class="xref docutils literal"><span class="pre">file</span></tt> type.  Use <a title="open" class="reference external" href="../library/functions.html#open"><tt class="xref docutils literal"><span class="pre">open()</span></tt></a>.  There are now several
different kinds of streams that open can return in the <a title="Core tools for working with streams." class="reference external" href="../library/io.html#module-io"><tt class="xref docutils literal"><span class="pre">io</span></tt></a> module.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">reduce()</span></tt>.  Use <a title="functools.reduce" class="reference external" href="../library/functools.html#functools.reduce"><tt class="xref docutils literal"><span class="pre">functools.reduce()</span></tt></a> if you really
need it; however, 99 percent of the time an explicit <a class="reference external" href="../reference/compound_stmts.html#for"><tt class="xref docutils literal"><span class="pre">for</span></tt></a>
loop is more readable.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">reload()</span></tt>.  Use <a title="imp.reload" class="reference external" href="../library/imp.html#imp.reload"><tt class="xref docutils literal"><span class="pre">imp.reload()</span></tt></a>.</li>
<li>Removed. <tt class="xref docutils literal"><span class="pre">dict.has_key()</span></tt> &#8211; use the <a class="reference external" href="../reference/expressions.html#in"><tt class="xref docutils literal"><span class="pre">in</span></tt></a> operator
instead.</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>Support for several platforms was dropped, including but not limited
to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.</li>
<li><span class="target" id="index-1177"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a>: New Buffer API.</li>
<li><span class="target" id="index-1178"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a>: Extension Module Initialization &amp; Finalization.</li>
<li><span class="target" id="index-1179"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3123"><strong>PEP 3123</strong></a>: Making <a title="PyObject_HEAD" class="reference external" href="../c-api/structures.html#PyObject_HEAD"><tt class="xref docutils literal"><span class="pre">PyObject_HEAD</span></tt></a> conform to standard C.</li>
<li>No more C API support for restricted execution.</li>
<li><tt class="xref docutils literal"><span class="pre">PyNumber_Coerce()</span></tt>, <tt class="xref docutils literal"><span class="pre">PyNumber_CoerceEx()</span></tt>,
<tt class="xref docutils literal"><span class="pre">PyMember_Get()</span></tt>, and <tt class="xref docutils literal"><span class="pre">PyMember_Set()</span></tt> C APIs are removed.</li>
<li>New C API <a title="PyImport_ImportModuleNoBlock" class="reference external" href="../c-api/import.html#PyImport_ImportModuleNoBlock"><tt class="xref docutils literal"><span class="pre">PyImport_ImportModuleNoBlock()</span></tt></a>, works like
<a title="PyImport_ImportModule" class="reference external" href="../c-api/import.html#PyImport_ImportModule"><tt class="xref docutils literal"><span class="pre">PyImport_ImportModule()</span></tt></a> but won&#8217;t block on the import lock
(returning an error instead).</li>
<li>Renamed the boolean conversion C-level slot and method:
<tt class="docutils literal"><span class="pre">nb_nonzero</span></tt> is now <tt class="docutils literal"><span class="pre">nb_bool</span></tt>.</li>
<li>Removed <tt class="xref docutils literal"><span class="pre">METH_OLDARGS</span></tt> and <tt class="xref docutils literal"><span class="pre">WITH_CYCLE_GC</span></tt> from the C API.</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&#8217;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>(Prerequisite:) Start with excellent test coverage.</li>
<li>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.</li>
<li>(Still using 2.6:) Turn on the <em class="xref">-3</em> 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.</li>
<li>Run the <tt class="docutils literal"><span class="pre">2to3</span></tt> source-to-source translator over your source code
tree.  (See <a class="reference external" href="../library/2to3.html#to3-reference"><em>2to3 - Automated Python 2 to 3 code translation</em></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.</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&#8217;d have to use a very contorted
coding style, e.g. avoiding <tt class="xref docutils literal"><span class="pre">print</span></tt> 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
<tt class="docutils literal"><span class="pre">2to3</span></tt> 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 external" href="../howto/cporting.html#cporting-howto"><em>Porting Extension Modules to 3.0</em></a>.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">What&#8217;s New In Python 3.0</a><ul>
<li><a class="reference external" href="#common-stumbling-blocks">Common Stumbling Blocks</a><ul>
<li><a class="reference external" href="#print-is-a-function">Print Is A Function</a></li>
<li><a class="reference external" href="#views-and-iterators-instead-of-lists">Views And Iterators Instead Of Lists</a></li>
<li><a class="reference external" href="#ordering-comparisons">Ordering Comparisons</a></li>
<li><a class="reference external" href="#integers">Integers</a></li>
<li><a class="reference external" 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 external" href="#overview-of-syntax-changes">Overview Of Syntax Changes</a><ul>
<li><a class="reference external" href="#new-syntax">New Syntax</a></li>
<li><a class="reference external" href="#changed-syntax">Changed Syntax</a></li>
<li><a class="reference external" href="#removed-syntax">Removed Syntax</a></li>
</ul>
</li>
<li><a class="reference external" href="#changes-already-present-in-python-2-6">Changes Already Present In Python 2.6</a></li>
<li><a class="reference external" href="#library-changes">Library Changes</a></li>
<li><a class="reference external" 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 external" href="#changes-to-exceptions">Changes To Exceptions</a></li>
<li><a class="reference external" href="#miscellaneous-other-changes">Miscellaneous Other Changes</a><ul>
<li><a class="reference external" href="#operators-and-special-methods">Operators And Special Methods</a></li>
<li><a class="reference external" href="#builtins">Builtins</a></li>
</ul>
</li>
<li><a class="reference external" href="#build-and-c-api-changes">Build and C API Changes</a></li>
<li><a class="reference external" href="#performance">Performance</a></li>
<li><a class="reference external" 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&#8217;s New In Python 3.1</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="2.7.html"
                                  title="next chapter">What&#8217;s New in Python 2.7</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/whatsnew/3.0.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <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="../modindex.html" title="Global 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="../index.html">Python v3.1.1 documentation</a> &raquo;</li>

          <li><a href="index.html" >What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>