finished 2to3 chapter

This commit is contained in:
Mark Pilgrim
2009-02-01 01:03:44 -05:00
parent 92af142439
commit 3d9df8a3aa
2 changed files with 164 additions and 174 deletions
+9 -5
View File
@@ -282,9 +282,10 @@ h3{margin-left:3.5em}
<li><a href="porting-code-to-python-3-with-2to3.html#exec"><code>exec</code> statement</a>
<li><a href="porting-code-to-python-3-with-2to3.html#execfile"><code>execfile</code> statement</a> (3.1+)
<li><a href="porting-code-to-python-3-with-2to3.html#repr"><code>repr</code> literals (backticks)</a>
<li><a href="porting-code-to-python-3-with-2to3.html#exceptions">Exceptions</a>
<li><a href="porting-code-to-python-3-with-2to3.html#except"><code>try...except</code> statement</a>
<li><a href="porting-code-to-python-3-with-2to3.html#raise"><code>raise</code> statement</a>
<li><a href="porting-code-to-python-3-with-2to3.html#throw"><code>throw</code> statement</a>
<li><a href="porting-code-to-python-3-with-2to3.html#throw"><code>throw</code> method on generators</a>
<li><a href="porting-code-to-python-3-with-2to3.html#xrange"><code>xrange()</code> global function</a>
<li><a href="porting-code-to-python-3-with-2to3.html#raw_input"><code>raw_input()</code> and <code>input()</code> global functions</a>
<li><a href="porting-code-to-python-3-with-2to3.html#funcattrs"><code>func_*</code> function attributes</a>
@@ -305,10 +306,13 @@ h3{margin-left:3.5em}
<li><a href="porting-code-to-python-3-with-2to3.html#paren">List comprehensions over tuples</a>
<li><a href="porting-code-to-python-3-with-2to3.html#getcwdu"><code>os.getcwdu()</code> function</a>
<li><a href="porting-code-to-python-3-with-2to3.html#metaclass">Metaclasses</a>
<li><a href="porting-code-to-python-3-with-2to3.html#set_literal"><code>set()</code> literals</a> (explicit)
<li><a href="porting-code-to-python-3-with-2to3.html#buffer"><code>buffer()</code> global function</a> (explicit)
<li><a href="porting-code-to-python-3-with-2to3.html#wscomma">Whitespace around commas</a> (explicit)
<li><a href="porting-code-to-python-3-with-2to3.html#idioms">Common idioms</a> (explicit)
<li><a href="porting-code-to-python-3-with-2to3.html#nitpick">Matters of style</a>
<ol>
<li><a href="porting-code-to-python-3-with-2to3.html#set_literal"><code>set()</code> literals</a>
<li><a href="porting-code-to-python-3-with-2to3.html#buffer"><code>buffer()</code> global function</a>
<li><a href="porting-code-to-python-3-with-2to3.html#wscomma">Whitespace around commas</a>
<li><a href="porting-code-to-python-3-with-2to3.html#idioms">Common idioms</a>
</ol>
</ol>
</div>
<p class="c">This site is optimized for Lynx just because fuck you.<br>I&#8217;m told it also looks good in graphical browsers.
+155 -169
View File
@@ -34,6 +34,7 @@ h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
<li><a href="#othermodules">Other modules</a>
</ol>
<li><a href="#import">Relative imports within a package</a>
<li><a href="#next"><code>next()</code> iterator method</a>
<li><a href="#filter"><code>filter()</code> global function</a>
<li><a href="#map"><code>map()</code> global function</a>
<li><a href="#reduce"><code>reduce()</code> global function</a> (3.1+)
@@ -42,16 +43,16 @@ h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
<li><a href="#exec"><code>exec</code> statement</a>
<li><a href="#execfile"><code>execfile</code> statement</a> (3.1+)
<li><a href="#repr"><code>repr</code> literals (backticks)</a>
<li><a href="#exceptions">Exceptions</a>
<li><a href="#except"><code>try...except</code> statement</a>
<li><a href="#raise"><code>raise</code> statement</a>
<li><a href="#throw"><code>throw</code> statement</a>
<li><a href="#throw"><code>throw</code> method on generators</a>
<li><a href="#xrange"><code>xrange()</code> global function</a>
<li><a href="#raw_input"><code>raw_input()</code> and <code>input()</code> global functions</a>
<li><a href="#funcattrs"><code>func_*</code> function attributes</a>
<li><a href="#xreadlines"><code>xreadlines()</code> I/O method</a>
<li><a href="#tuple_params"><code>lambda</code> functions with multiple parameters</a>
<li><a href="#methodattrs">Special method attributes</a>
<li><a href="#next"><code>next()</code> iterator method</a>
<li><a href="#nonzero"><code>__nonzero__</code> special class attribute</a>
<li><a href="#numliterals">Octal literals</a>
<li><a href="#renames"><code>sys.maxint</code></a>
@@ -66,10 +67,13 @@ h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
<li><a href="#paren">List comprehensions over tuples</a>
<li><a href="#getcwdu"><code>os.getcwdu()</code> function</a>
<li><a href="#metaclass">Metaclasses</a>
<li><a href="#set_literal"><code>set()</code> literals</a> (explicit)
<li><a href="#buffer"><code>buffer()</code> global function</a> (explicit)
<li><a href="#wscomma">Whitespace around commas</a> (explicit)
<li><a href="#idioms">Common idioms</a> (explicit)
<li><a href="#nitpick">Matters of style</a>
<ol>
<li><a href="#set_literal"><code>set()</code> literals</a>
<li><a href="#buffer"><code>buffer()</code> global function</a>
<li><a href="#wscomma">Whitespace around commas</a>
<li><a href="#idioms">Common idioms</a>
</ol>
</ol>
<h2 id="divingin">Diving in</h2>
<p class="fancy">Python 3 comes with a utility script called <code>2to3</code>, which takes your actual Python 2 source code as input and auto-converts as much as it can to Python 3. <a href="case-study-porting-chardet-to-python-3.html#running2to3">Case study: porting <code>chardet</code> to Python 3</a> describes how to run the <code>2to3</code> script, then shows some things it can't fix automatically. This appendix documents what it <em>can</em> fix automatically.
@@ -252,7 +256,7 @@ h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
</ol>
<h2 id="imports">Modules that have been renamed or reorganized</h2>
<p>Several modules in the Python Standard Library have been renamed. Several other modules which are related to each other have been combined or reorganized to make their association more logical.
<h3 id="http"><code>http</code> package</h3>
<h3 id="http"><code>http</code></h3>
<p>In Python 3, several related HTTP modules have been combined into a single package, <code>http</code>.
<p class="skip"><a href="#skipcompareimporthttp">skip over this table</a>
<table id="compareimporthttp">
@@ -281,7 +285,7 @@ import CGIHttpServer</code></pre></td>
<li>The <code>http.cookiejar</code> module manipulates the actual files on disk that popular web browsers use to store cookies.
<li>The <code>http.server</code> module provides a basic HTTP server.
</ol>
<h3 id="urllib"><code>urllib</code> package</h3>
<h3 id="urllib"><code>urllib</code></h3>
<p>Python 2 had a rat's nest of overlapping modules to parse, encode, and fetch URLs. In Python 3, these have all been refactored and combined in a single package, <code>urllib</code>.
<p class="skip"><a href="#skipcompareimporturllib">skip over this table</a>
<table id="compareimporturllib">
@@ -320,7 +324,7 @@ from urllib.error import HTTPError</code></pre></td></tr>
<li>The <code>FancyURLopener</code> class, which handles HTTP redirects and other status codes, is still available in the new <code>urllib.request</code> module. The <code>urlencode</code> function has moved to <code>urllib.parse</code>.
<li>The <code>Request</code> object is still available in <code>urllib.request</code>, but constants like <code>HTTPError</code> have been moved to <code>urllib.error</code>.
</ol>
<h3 id="dbm"><code>dbm</code> package</h3>
<h3 id="dbm"><code>dbm</code></h3>
<p>All the various DBM clones are now in a single package, <code>dbm</code>. If you need a specific variant like GNU DBM, you can import the appropriate module within the <code>dbm</code> package.
<p class="skip"><a href="#skipcompareimportdbm">skip over this table</a>
<table id="compareimportdbm">
@@ -346,7 +350,7 @@ import whichdb</code></pre></td>
<td><code>import dbm</code></td></tr>
</table>
<p id="skipcompareimportdbm">
<h3 id="xmlrpc"><code>xmlrpc</code> package</h3>
<h3 id="xmlrpc"><code>xmlrpc</code></h3>
<p>XML-RPC is a lightweight method of performing remote RPC calls over HTTP. The XML-RPC client library and several XML-RPC server implementations are now combined in a single package, <code>xmlrpc</code>.
<p class="skip"><a href="#skipcompareimportxmlrpc">skip over this table</a>
<table id="compareimportxmlrpc">
@@ -446,6 +450,47 @@ except ImportError:
<li>When you need to import an entire module from elsewhere in your package, use the new <code>from . import</code> syntax. The period is actually a relative path from this file (<code>universaldetector.py</code>) to the file you want to import (<code>constants.py</code>). In this case, they are in the same directory, thus the single period. You can also import from the parent directory (<code>from .. import anothermodule</code>) or a subdirectory.
<li>To import a specific class or function from another module directly into your module's namespace, prefix the target module with a relative path, minus the trailing slash. In this case, <code>mbcharsetprober.py</code> is in the same directory as <code>universaldetector.py</code>, so the path is a single period. You can also import form the parent directory (<code>from ..anothermodule import AnotherClass</code>) or a subdirectory.
</ol>
<h2 id="next"><code>next()</code> iterator method</h2>
<p>In Python 2, iterators had a <code>next()</code> method which returned the next item in the sequence. That's still true in Python 3, but there is now also a global <code>next()</code> function that takes an iterator as an argument.
<p class="skip"><a href="#skipcomparenext">skip over this table</a>
<table id="comparenext">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<td><code>anIterator.next()</code></td>
<td><code>next(anIterator)</code></td></tr>
<tr><th>&#x2461;</th>
<td><code>a_function_that_returns_an_iterator().next()</code></td>
<td><code>next(a_function_that_returns_an_iterator())</code></td></tr>
<tr><th>&#x2462;</th>
<td><pre><code>class A:
def next(self):
pass</code></pre></td>
<td><pre><code>class A:
def __next__(self):
pass</code></pre></td></tr>
<tr><th>&#x2463;</th>
<td><pre><code>class A:
def next(self, x, y):
pass</code></pre></td>
<td><i>no change</i></td></tr>
<tr><th>&#x2464;</th>
<td><pre><code>next = 42
for an_iterator in a_sequence_of_iterators:
an_iterator.next()</code></pre></td>
<td><pre><code>next = 42
for an_iterator in a_sequence_of_iterators:
an_iterator.__next__()</code></pre></td></tr>
</table>
<ol id="skipcomparenext">
<li>In the simplest case, instead of calling an iterator's <code>next()</code> method, you now pass the iterator itself to the global <code>next()</code> function.
<li>If you have a function that returns an iterator, call the function and pass the result to the <code>next()</code> function. (The <code>2to3</code> script is smart enough to convert this properly.)
<li>If you define your own class and mean to use it as an iterator, define the <code>__next__()</code> special method.
<li>If you define your own class and just happen to have a method named <code>next()</code> that takes one or more arguments, <code>2to3</code> will not touch it. This class can not be used as an iterator, because its <code>next()</code> method takes arguments.
<li>This one is a bit tricky. If you have a local variable named <var>next</var>, then it takes precedence over the new global <code>next()</code> function. In this case, you need to call the iterator's special <code>__next()__</code> method to get the next item in the sequence. (Alternatively, you could also refactor the code so the local variable wasn't named <var>next</var>, but <code>2to3</code> will not do that for you automatically.)
</ol>
<h2 id="filter"><code>filter()</code> global function</h2>
<p>In Python 2, the <code>filter()</code> function returned a list, the result of filtering a sequence through a function that returned <code>True</code> or <code>False</code> for each item in the sequence. In Python 3, the <code>filter()</code> function returns an interator, not a list.
<p class="skip"><a href="#skipcomparefilter">skip over this table</a>
@@ -624,7 +669,7 @@ reduce(a, b, c)</code></pre></td></tr>
<li>In Python 2, backticks could be nested, leading to this sort of confusing (but valid) expression. The <code>2to3</code> tool is smart enough to convert this into nested calls to <code>repr()</code>.
</ol>
<h2 id="except"><code>try...except</code> statement</h2>
<p>The syntax for catching exceptions has changed slightly between Python 2 and 3.
<p>The syntax for catching exceptions has changed slightly between Python 2 and Python 3.
<p class="skip"><a href="#skipcompareexcept">skip over this table</a>
<table id="compareexcept">
<tr><th>Notes</th>
@@ -665,7 +710,7 @@ except:
<ol id="skipcompareexcept">
<li>Instead of a comma after the exception type, Python 3 uses a new keyword, <code>as</code>.
<li>The <code>as</code> keyword also works for catching multiple types of exceptions at once.
<li>If you catch an exception but don't actually care about accessing the exception object itself, the syntax is identical between Python 2 and 3.
<li>If you catch an exception but don't actually care about accessing the exception object itself, the syntax is identical between Python 2 and Python 3.
<li>Similarly, if you use a fallback to catch <em>all</em> exceptions, the syntax is identical.
</ol>
<blockquote class="note">
@@ -699,8 +744,8 @@ except:
<li>Python 2 supported a more complex syntax to raise an exception with a custom traceback (stack trace). You can do this in Python 3 as well, but the syntax is quite different.
<li>In Python 2, you could raise an exception with no exception class, just an error message. In Python 3, this is no longer possible. <code>2to3</code> will warn you that it was unable to fix this automatically.
</ol>
<h2 id="throw"><code>throw</code> statement</h2>
<p>FIXME intro
<h2 id="throw"><code>throw</code> method on generators</h2>
<p>In Python 2, generators have a <code>throw()</code> method. Calling <code>a_generator.throw()</code> raises an exception at the point where the generator was paused, then returns the next value yielded by the generator function. In Python 3, this functionality is still available, but the syntax is slightly different.
<p class="skip"><a href="#skipcomparethrow">skip over this table</a>
<table id="comparethrow">
<tr><th>Notes</th>
@@ -708,19 +753,19 @@ except:
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<td><code>aGenerator.throw(MyException)</code></td>
<td><code>a_generator.throw(MyException)</code></td>
<td><i>no change</i></td></tr>
<tr><th>&#x2461;</th>
<td><code>aGenerator.throw(MyException, "error message")</code></td>
<td><code>aGenerator.throw(MyException("error message"))</code></td></tr>
<td><code>a_generator.throw(MyException, "error message")</code></td>
<td><code>a_generator.throw(MyException("error message"))</code></td></tr>
<tr><th>&#x2462;</th>
<td><code>aGenerator.throw("error message")</code></td>
<td><code>a_generator.throw("error message")</code></td>
<td><i>unsupported</i></td></tr>
</table>
<ol id="skipcomparethrow">
<li>...
<li>...
<li>...
<li>In the simplest form, a generator throws an exception without a custom error message. In this case, the syntax has not changed between Python 2 and Python 3.
<li>If the generator throws an exception <em>with</em> a custom error message, you need to pass the error string to the exception when you create it.
<li>Python 2 also supported throwing an exception with <em>only</em> a custom error message. Python 3 does not support this, and the <code>2to3</code> script will display a warning telling you that you will need to fix this code manually.
</ol>
<h2 id="xrange"><code>xrange()</code> global function</h2>
<p>In Python 2, there were two ways to get a range of numbers: <code>range()</code>, which returned a list, and <code>xrange()</code>, which returned an iterator. In Python 3, <code>range()</code> returns an iterator, and <code>xrange()</code> doesn't exist.
@@ -777,7 +822,7 @@ except:
<li>If you actually need to ask the user for a Python expression to evaluate, use the <code>input()</code> function and pass the result to <code>eval()</code>.
</ol>
<h2 id="funcattrs"><code>func_*</code> function attributes</h2>
<p>FIXME intro
<p>In Python 2, code within functions can access special attributes about the function itself. In Python 3, these special function attributes have been renamed for consistency with other attributes.
<p class="skip"><a href="#skipcomparefuncattrs">skip over this table</a>
<table id="comparefuncattrs">
<tr><th>Notes</th>
@@ -785,38 +830,38 @@ except:
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<td><code>a_function.func_closure</code></td>
<td><code>a_function.__closure__</code></td></tr>
<td><code>a_function.func_name</code></td>
<td><code>a_function.__name__</code></td></tr>
<tr><th>&#x2461;</th>
<td><code>a_function.func_doc</code></td>
<td><code>a_function.__doc__</code></td></tr>
<tr><th>&#x2462;</th>
<td><code>a_function.func_name</code></td>
<td><code>a_function.__name__</code></td></tr>
<tr><th>&#x2463;</th>
<td><code>a_function.func_defaults</code></td>
<td><code>a_function.__defaults__</code></td></tr>
<tr><th>&#x2463;</th>
<td><code>a_function.func_dict</code></td>
<td><code>a_function.__dict__</code></td></tr>
<tr><th>&#x2464;</th>
<td><code>a_function.func_code</code></td>
<td><code>a_function.__code__</code></td></tr>
<td><code>a_function.func_closure</code></td>
<td><code>a_function.__closure__</code></td></tr>
<tr><th>&#x2465;</th>
<td><code>a_function.func_globals</code></td>
<td><code>a_function.__globals__</code></td></tr>
<tr><th>&#x2466;</th>
<td><code>a_function.func_dict</code></td>
<td><code>a_function.__dict__</code></td></tr>
<td><code>a_function.func_code</code></td>
<td><code>a_function.__code__</code></td></tr>
</table>
<ol id="skipcomparefuncattrs">
<li>...
<li>...
<li>...
<li>...
<li>...
<li>...
<li>...
<li>The <code>__name__</code> attribute (previously <code>func_name</code>) contains the function's name.
<li>The <code>__doc__</code> attribute (previously <code>func_doc</code>) contains the <i>docstring</i> that you defined in the function's source code.
<li>The <code>__defaults__</code> attribute (previously <code>func_defaults</code>) is a tuple containing default argument values for those arguments that have default values.
<li>The <code>__dict__</code> attribute (previously <code>func_dict</code>) is the namespace supporting arbitrary function attributes.
<li>The <code>__closure__</code> attribute (previously <code>func_closure</code>) is a tuple of cells that contain bindings for the function's free variables.
<li>The <code>__globals__</code> attribute (previously <code>func_globals</code>) is a reference to the global namespace of the module in which the function was defined.
<li>The <code>__code__</code> attribute (previously <code>func_code</code>) is a code object representing the compiled function body.
</ol>
<h2 id="xreadlines"><code>xreadlines()</code> I/O method</h2>
<p>In Python 2, file objects had an <code>xreadlines()</code> method which returned an iterator that would read the file one line at a time. This was useful in <code>for</code> loops, among other places. In fact, it was so useful, later versions of Python 2 [FIXME exact version?] added the capability to file objects themselves.
<p>In Python 2, file objects had an <code>xreadlines()</code> method which returned an iterator that would read the file one line at a time. This was useful in <code>for</code> loops, among other places. In fact, it was so useful, later versions of Python 2 added the capability to file objects themselves.
<p class="skip"><a href="#skipcomparexreadlines">skip over this table</a>
<table id="comparexreadlines">
<tr><th>Notes</th>
@@ -835,7 +880,7 @@ except:
<li>If you used to call <code>xreadlines()</code> with an argument (the number of lines to read at a time), keep doing that. It still works in Python 3, and <code>2to3</code> will not change it.
</ol>
<h2 id="tuple_params"><code>lambda</code> functions with multiple parameters</h2>
<p>FIXME intro
<p>In Python 2, you could define anonymous <code>lambda</code> functions which took multiple parameters by defining the function as taking a tuple with a specific number of items. In effect, Python 2 would &#8220;unpack&#8221; the tuple into named arguments, which you could then reference (by name) within the <code>lambda</code> function. In Python 3, you can still pass a tuple to a <code>lambda</code> function, but the Python interpreter will not unpack the tuple into named arguments. Instead, you will need to reference each argument by its positional index.
<p class="skip"><a href="#skipcomparetuple_params">skip over this table</a>
<table id="comparetuple_params">
<tr><th>Notes</th>
@@ -844,7 +889,7 @@ except:
</tr>
<tr><th>&#x2460;</th>
<td><code>lambda (x,): x + f(x)</code></td>
<td><code>lambda x1: x1[0] + f(x1[1])</code></td></tr>
<td><code>lambda x1: x1[0] + f(x1[0])</code></td></tr>
<tr><th>&#x2461;</th>
<td><code>lambda (x, y): x + f(y)</code></td>
<td><code>lambda x_y: x_y[0] + f(x_y[1])</code></td></tr>
@@ -853,12 +898,12 @@ except:
<td><code>lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]</code></td></tr>
</table>
<ol id="skipcomparetuple_params">
<li>...
<li>...
<li>...
<li>If you had defined a <code>lambda</code> function that took a tuple of one item, in Python 3 that would become a <code>lambda</code> with references to <var>x1[0]</var>. The name <var>x1</var> is autogenerated by the <code>2to3</code> script, based on the named arguments in the original tuple.
<li>A <code>lambda</code> function with a two-item tuple <var>(x, y)</var> gets converted to <var>x_y</var> with positional arguments <var>x_y[0]</var> and <var>x_y[1]</var>.
<li>The <code>2to3</code> script can even handle <code>lambda</code> functions with nested tuples of named arguments. The resulting Python 3 code is a bit unreadable, but it works the same as the old code did in Python 2.
</ol>
<h2 id="methodattrs">Special method attributes</h2>
<p>FIXME intro
<p>In Python 2, class methods can reference the class object they are defined in, as well as the method object itself. <code>im_self</code> is the class instance object; the class <code>im_func</code> is the function object; <code>im_class</code> is the class of <code>im_self</code> (for bound methods) or the class that asked for the method (for unbound methods). In Python 3, these special method attributes have been renamed to follow the naming conventions of other attributes.
<p class="skip"><a href="#skipcomparemethodattrs">skip over this table</a>
<table id="comparemethodattrs">
<tr><th>Notes</th>
@@ -875,54 +920,9 @@ except:
<td><code>aClassInstance.aClassMethod.im_class</code></td>
<td><code>aClassInstance.aClassMethod.self.__class__</code></td></tr>
</table>
<ol id="skipcomparemethodattrs">
<li>...
<li>...
<li>...
</ol>
<h2 id="next"><code>next()</code> iterator method</h2>
<p>In Python 2, iterators had a <code>next()</code> method which returned the next item in the sequence. That's still true in Python 3, but there is now also a global <code>next()</code> function that takes an iterator as an argument.
<p class="skip"><a href="#skipcomparenext">skip over this table</a>
<table id="comparenext">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<td><code>anIterator.next()</code></td>
<td><code>next(anIterator)</code></td></tr>
<tr><th>&#x2461;</th>
<td><code>a_function_that_returns_an_iterator().next()</code></td>
<td><code>next(a_function_that_returns_an_iterator())</code></td></tr>
<tr><th>&#x2462;</th>
<td><pre><code>class A:
def next(self):
pass</code></pre></td>
<td><pre><code>class A:
def __next__(self):
pass</code></pre></td></tr>
<tr><th>&#x2463;</th>
<td><pre><code>class A:
def next(self, x, y):
pass</code></pre></td>
<td><i>no change</i></td></tr>
<tr><th>&#x2464;</th>
<td><pre><code>next = 42
for an_iterator in a_sequence_of_iterators:
an_iterator.next()</code></pre></td>
<td><pre><code>next = 42
for an_iterator in a_sequence_of_iterators:
an_iterator.__next__()</code></pre></td></tr>
</table>
<ol id="skipcomparenext">
<li>In the simplest case, instead of calling an iterator's <code>next()</code> method, you now pass the iterator itself to the global <code>next()</code> function.
<li>If you have a function that returns an iterator, call the function and pass the result to the <code>next()</code> function. (The <code>2to3</code> script is smart enough to convert this properly.)
<li>If you define your own class and mean to use it as an iterator, define the <code>__next__()</code> special method.
<li>If you define your own class and just happen to have a method named <code>next()</code> that takes one or more arguments, <code>2to3</code> will not touch it. This class can not be used as an iterator, because its <code>next()</code> method takes arguments.
<li>This one is a bit tricky. If you have a local variable named <var>next</var>, then it takes precedence over the new global <code>next()</code> function. In this case, you need to call the iterator's special <code>__next()__</code> method to get the next item in the sequence. (Alternatively, you could also refactor the code so the local variable wasn't named <var>next</var>, but <code>2to3</code> will not do that for you automatically.)
</ol>
<p id="skipcomparemethodattrs">
<h2 id="nonzero"><code>__nonzero__</code> special class attribute</h2>
<p>FIXME intro
<p>In Python 2, you could build your own classes that could be used in a boolean context. For example, you could instantiate the class and then use the instance in an <code>if</code> statement. To do this, you defined a special <code>__nonzero__()</code> method which returned <code>True</code> or <code>False</code>, and it was called whenever the instance was used in a boolean context. In Python 3, you can still do this, but the name of the method has changed to <code>__bool__()</code>.
<p class="skip"><a href="#skipcomparenonzero">skip over this table</a>
<table id="comparenonzero">
<tr><th>Notes</th>
@@ -943,8 +943,8 @@ for an_iterator in a_sequence_of_iterators:
<td><i>no change</i></td></tr>
</table>
<ol id="skipcomparenonzero">
<li>...
<li>...
<li>Instead of <code>__nonzero__()</code>, Python 3 calls the <code>__bool__()</code> method when evaluating an instance in a boolean context.
<li>However, if you have a <code>__nonzero__()</code> method that takes arguments, the <code>2to3</code> tool will assume that you were using it for some other purpose, and it will not make any changes.
</ol>
<h2 id="numliterals">Octal literals</h2>
<p>The syntax for defining base 8 (octal) numbers has changed slightly between Python 2 and Python 3.
@@ -992,7 +992,7 @@ for an_iterator in a_sequence_of_iterators:
</table>
<p id="skipcomparecallable">
<h2 id="zip"><code>zip()</code> global function</h2>
<p>FIXME intro
<p>In Python 2, the global <code>zip()</code> function took any number of sequences and returned a list of tuples. The first tuple contained the first item from each sequence; the second tuple contained the second item from each sequence; and so on. In Python 3, <code>zip()</code> returns an iterator instead of a list.
<p class="skip"><a href="#skipcomparezip">skip over this table</a>
<table id="comparezip">
<tr><th>Notes</th>
@@ -1007,28 +1007,25 @@ for an_iterator in a_sequence_of_iterators:
<td><i>no change</i></td></tr>
</table>
<ol id="skipcomparezip">
<li>...
<li>...
<li>In the simplest form, you can get the old behavior of the <code>zip()</code> function by wrapping the return value in a call to <code>list()</code>, which will run through the iterator that <code>zip()</code> returns and return a real list of the results.
<li>In contexts that already iterate through all the items of a sequence (such as this call to the <code>join()</code> method), the iterator that <code>zip()</code> returns will work just fine. The <code>2to3</code> script is smart enough to detect these cases and make no change to your code.
</ol>
<h2 id="standarderror"><code>StandardError()</code> exception</h2>
<p>FIXME intro
<h2 id="standarderror"><code>StandardError</code> exception</h2>
<p>In Python 2, <code>StandardError</code> was the base class for all built-in exceptions other than <code>StopIteration</code>, <code>GeneratorExit</code>, <code>KeyboardInterrupt</code>, and <code>SystemExit</code>. In Python 3, <code>StandardError</code> has been eliminated; use <code>Exception</code> instead.
<p class="skip"><a href="#skipcomparestandarderror">skip over this table</a>
<table id="comparestandarderror">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><code>x = StandardError()</code></td>
<td><code>x = Exception()</code></td></tr>
<tr><th>&#x2461;</th>
<tr><th></th>
<td><code>x = StandardError(a, b, c)</code></td>
<td><code>x = Exception(a, b, c)</code></td></tr>
</table>
<ol id="skipcomparestandarderror">
<li>...
<li>...
</ol>
<p id="skipcomparestandarderror">
<h2 id="types"><code>types</code> module constants</h2>
<p>The <code>types</code> module contains a variety of constants to help you determine the type of an object. In Python 2, it contained constants for all primitive types like <code>dict</code> and <code>int</code>. In Python 3, these constants have been eliminated; just use the primitive type name instead.
<p class="skip"><a href="#skipcomparetypes">skip over this table</a>
@@ -1087,7 +1084,7 @@ for an_iterator in a_sequence_of_iterators:
</table>
<p id="skipcomparebasestring">
<h2 id="itertools"><code>itertools</code> module</h2>
<p>FIXME intro
<p>Python 2.3 introduced the <code>itertools</code> module, which defined variants of the global <code>zip()</code>, <code>map()</code>, and <code>filter()</code> functions that returned iterators instead of lists. In Python 3, those global functions return iterators, so those functions in the <code>itertools</code> module have been eliminated.
<table id="compareitertools">
<tr><th>Notes</th>
<th>Python 2</th>
@@ -1103,18 +1100,14 @@ for an_iterator in a_sequence_of_iterators:
<td><code>itertools.ifilter(a, b)</code></td>
<td><code>filter(a, b)</code></td></tr>
<tr><th>&#x2463;</th>
<td><code>itertools.ifilterfalse(a, b)</code></td>
<td><code>filterfalse(a, b)</code></td></tr>
<tr><th>&#x2464;</th>
<td><code>from itertools import imap, izip, foo</code></td>
<td><code>from itertools import foo</code></td></tr>
</table>
<ol id="skipcompareitertools">
<li>...
<li>...
<li>...
<li>...
<li>...
<li>Instead of <code>itertools.izip()</code>, just use the global <code>zip()</code> function.
<li>Instead of <code>itertools.imap()</code>, just use <code>map()</code>.
<li><code>itertools.ifilter()</code> becomes <code>filter()</code>.
<li>The <code>itertools</code> module still exists in Python 3, it just doesn't have the functions that have migrated to the global namespace. The <code>2to3</code> script is smart enough to remove the specific imports that no longer exist, while leaving other imports intact.
</ol>
<h2 id="sys_exc"><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></h2>
<p>Python 2 had three variables in the <code>sys</code> module that you could access while an exception was being handled: <code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code>. (Actually, these date all the way back to Python 1.) Ever since Python 1.5, these variables have been deprecated in favor of <code>sys.exc_info</code>, which is a tuple that contains all three values. In Python 3, these individual variables have finally gone away; you must use <code>sys.exc_info</code>.
@@ -1124,19 +1117,19 @@ for an_iterator in a_sequence_of_iterators:
<th>Python 2</th>
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><code>sys.exc_type</code></td>
<td><code>sys.exc_info()[0]</code></td></tr>
<tr><th>&#x2461;</th>
<tr><th></th>
<td><code>sys.exc_value</code></td>
<td><code>sys.exc_info()[1]</code></td></tr>
<tr><th>&#x2462;</th>
<tr><th></th>
<td><code>sys.exc_traceback</code></td>
<td><code>sys.exc_info()[2]</code></td></tr>
</table>
<p id="skipcomparesys_exc">
<h2 id="paren">List comprehensions over tuples</h2>
<p>FIXME intro
<p>In Python 2, if you wanted to code a list comprehension that iterated over a tuple, you did not need to put parentheses around the tuple values. In Python 3, explicit parentheses are required.
<p class="skip"><a href="#skipcompareparen">skip over this table</a>
<table id="compareparen">
<tr><th>Notes</th>
@@ -1149,7 +1142,7 @@ for an_iterator in a_sequence_of_iterators:
</table>
<p id="skipcompareparen">
<h2 id="getcwdu"><code>os.getcwdu()</code> function</h2>
<p>Python 2 had a function named <code>os.getcwd()</code>, which returned the current working directory as a (non-Unicode) string. Because modern file systems can handle directory names in any character encoding, Python 2.3 introduced <code>os.getcwdu()</code>. The <code>os.getcwdu()</code> function returned the current working directory as a Unicode string. In Python 3, there is only one string type, so <code>os.getcwd()</code> is all you need.
<p>Python 2 had a function named <code>os.getcwd()</code>, which returned the current working directory as a (non-Unicode) string. Because modern file systems can handle directory names in any character encoding, Python 2.3 introduced <code>os.getcwdu()</code>. The <code>os.getcwdu()</code> function returned the current working directory as a Unicode string. In Python 3, there is only one string type (Unicode), so <code>os.getcwd()</code> is all you need.
<p class="skip"><a href="#skipcomparegetcwdu">skip over this table</a>
<table id="comparegetcwdu">
<tr><th>Notes</th>
@@ -1162,7 +1155,7 @@ for an_iterator in a_sequence_of_iterators:
</table>
<p id="skipcomparegetcwdu">
<h2 id="metaclass">Metaclasses</h2>
<p>FIXME intro
<p>In Python 2, you could create metaclasses either by defining the <code>metaclass</code> argument in the class declaration, or by defining a special class-level <code>__metaclass__</code> attribute. In Python 3, the class-level attribute has been eliminated.
<p class="skip"><a href="#skipcomparemetaclass">skip over this table</a>
<table id="comparemetaclass">
<tr><th>Notes</th>
@@ -1170,22 +1163,29 @@ for an_iterator in a_sequence_of_iterators:
<th>Python 3</th>
</tr>
<tr><th>&#x2460;</th>
<td><pre><code>class C(metaclass=PapayaMeta):
pass</code></pre></td>
<td><i>unchanged</i></td></tr>
<tr><th>&#x2461;</th>
<td><pre><code>class Whip:
__metaclass__ = PapayaMeta</code></pre></td>
<td><pre><code>class Whip(metaclass=PapayaMeta):
pass</code></pre></td></tr>
<tr><th>&#x2461;</th>
<td><pre><code>class C(Whipper):
<tr><th>&#x2462;</th>
<td><pre><code>class C(Whipper, Beater):
__metaclass__ = PapayaMeta</code></pre></td>
<td><pre><code>class C(Whipper, metaclass=PapayaMeta):
<td><pre><code>class C(Whipper, Beater, metaclass=PapayaMeta):
pass</code></pre></td></tr>
</table>
<ol id="skipcomparemetaclass">
<li>...
<li>...
<li>Declaring the metaclass in the class declaration worked in Python 2, and it still works the same in Python 3.
<li>Declaring the metaclass in a class attribute worked in Python 2, but doesn't work in Python 3.
<li>The <code>2to3</code> script is smart enough to construct a valid class declaration, even if the class is inherited from one or more base classes.
</ol>
<h2 id="set_literal"><code>set()</code> literals (explicit)</h2>
<p>FIXME intro
<h2 id="nitpick">Matters of style</h2>
<p>The rest of the &#8220;fixes&#8221; listed here aren't really fixes per se. That is, the things they change are matters of style, not substance. They work just as well in Python 3 as they do in Python 2, but the developers of Python have a vested interest in making Python code as uniform as possible. To that end, there is an <a href="http://www.python.org/dev/peps/pep-0008/">official Python style guide</a> which outlines &mdash; in excruciating detail &mdash; all sorts of nitpicky details that you almost certainly don't care about. And given that <code>2to3</code> provides such a great infrastructure for converting Python code from one thing to another, the authors took it upon themselves to add a few optional features to improve the readability of your Python programs.
<h3 id="set_literal"><code>set()</code> literals (explicit)</h3>
<p>In Python 2, the only way to define a literal set in your code was to call <code>set(a_sequence)</code>. This still works in Python 3, but a clearer way of doing it is to use the new set literal notation: curly braces. (Dictionaries are also defined with curly braces, which makes sense once you think about it, because dictionaries are just sets of key-value pairs.)
<blockquote class="note">
<p>&#x261E;
<p>The <code>2to3</code> script will not fix <code>set()</code> literals by default. To enable this fix, specify <kbd>-f set_literal</kbd> on the command line when you call <code>2to3</code>.
@@ -1193,26 +1193,22 @@ for an_iterator in a_sequence_of_iterators:
<p class="skip"><a href="#skipcompareset_literal">skip over this table</a>
<table id="compareset_literal">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
<th>Before</th>
<th>After</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><code>set([1, 2, 3])</code></td>
<td><code>{1, 2, 3}</code></td></tr>
<tr><th>&#x2461;</th>
<tr><th></th>
<td><code>set((1, 2, 3))</code></td>
<td><code>{1, 2, 3}</code></td></tr>
<tr><th>&#x2462;</th>
<tr><th></th>
<td><code>set([i for i in a_sequence])</code></td>
<td><code>{i for i in a_sequence}</code></td></tr>
</table>
<ol id="skipcompareset_literal">
<li>...
<li>...
<li>...
</ol>
<h2 id="buffer"><code>buffer()</code> global function (explicit)</h2>
<p>FIXME intro
<p id="skipcompareset_literal">
<h3 id="buffer"><code>buffer()</code> global function (explicit)</h3>
<p>Python objects implemented in C can export a &#8220;buffer interface,&#8221; which is a block of memory that is directly readable and writeable without copying. (That is exactly as powerful and scary as it sounds.) In Python 3, <code>buffer()</code> has been renamed to <code>memoryview()</code>. (It's a little more complicated than that, but you can almost certainly ignore the differences.)
<blockquote class="note">
<p>&#x261E;
<p>The <code>2to3</code> script will not fix the <code>buffer()</code> function by default. To enable this fix, specify <kbd>-f buffer</kbd> on the command line when you call <code>2to3</code>.
@@ -1220,18 +1216,16 @@ for an_iterator in a_sequence_of_iterators:
<p class="skip"><a href="#skipcomparebuffer">skip over this table</a>
<table id="comparebuffer">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
<th>Before</th>
<th>After</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><code>x = buffer(y)</code></td>
<td><code>x = memoryview(y)</code></td></tr>
</table>
<ol id="skipcomparebuffer">
<li>...
</ol>
<h2 id="wscomma">Whitespace around commas (explicit)</h2>
<p>FIXME intro
<p id="skipcomparebuffer">
<h3 id="wscomma">Whitespace around commas (explicit)</h3>
<p>Despite being draconian about whitespace for indenting and outdenting, Python is actually quite liberal about whitespace in other areas. Within lists, tuples, sets, and dictionaries, whitespace can appear before and after commas with no ill effects. However, the Python style guide states that commas should be preceded by zero spaces and followed by one. Although this is purely an aesthetic issue (the code works either way, in both Python 2 and Python 3), the <code>2to3</code> script can optionally fix this for you.
<blockquote class="note">
<p>&#x261E;
<p>The <code>2to3</code> script will not fix whitespace around commas by default. To enable this fix, specify <kbd>-f wscomma</kbd> on the command line when you call <code>2to3</code>.
@@ -1239,22 +1233,19 @@ for an_iterator in a_sequence_of_iterators:
<p class="skip"><a href="#skipcomparewscomma">skip over this table</a>
<table id="comparewscomma">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
<th>Before</th>
<th>After</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><code>a ,b</code></td>
<td><code>a, b</code></td></tr>
<tr><th>&#x2461;</th>
<tr><th></th>
<td><code>{a :b}</code></td>
<td><code>{a: b}</code></td></tr>
</table>
<ol id="skipcomparewscomma">
<li>...
<li>...
</ol>
<h2 id="idioms">Common idioms (explicit)</h2>
<p>FIXME intro
<p id="skipcomparewscomma">
<h3 id="idioms">Common idioms (explicit)</h3>
<p>There were a number of common idioms built up in the Python community. Some, like the <code>while 1:</code> loop, date back to Python 1. (Python didn't have a true boolean type until version 2.3, so developers used <code>1</code> and <code>0</code> instead.) Modern Python programmers should train their brains to use modern versions of these idioms instead.
<blockquote class="note">
<p>&#x261E;
<p>The <code>2to3</code> script will not fix common idioms by default. To enable this fix, specify <kbd>-f idioms</kbd> on the command line when you call <code>2to3</code>.
@@ -1262,33 +1253,28 @@ for an_iterator in a_sequence_of_iterators:
<p class="skip"><a href="#skipcompareidioms">skip over this table</a>
<table id="compareidioms">
<tr><th>Notes</th>
<th>Python 2</th>
<th>Python 3</th>
<th>Before</th>
<th>After</th>
</tr>
<tr><th>&#x2460;</th>
<tr><th></th>
<td><pre><code>while 1:
do_stuff()</code></pre></td>
<td><pre><code>while True:
do_stuff()</code></pre></td></tr>
<tr><th>&#x2461;</th>
<tr><th></th>
<td><code>type(x) == T</code></td>
<td><code>isinstance(x, T)</code></td></tr>
<tr><th>&#x2462;</th>
<tr><th></th>
<td><code>type(x) is T</code></td>
<td><code>isinstance(x, T)</code></td></tr>
<tr><th>&#x2463;</th>
<tr><th></th>
<td><pre><code>a_list = list(a_sequence)
a_list.sort()
do_stuff(a_list)</code></pre></td>
<td><pre><code>a_list = sorted(a_sequence)
do_stuff(a_list)</code></pre></td></tr>
</table>
<ol id="skipcompareidioms">
<li>...
<li>...
<li>...
<li>...
</ol>
<p id="skipcompareidioms">
<p>FIXME: once the rest of the book is written, this appendix should contain copious links back to any chapter or section that touches on these features.
<p class="c">&copy; 2001-4, 2009 <span>&#x2133;</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
<script type="text/javascript">