mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
two more 2to3 sections, fixed TOC
This commit is contained in:
+55
-51
@@ -255,57 +255,61 @@ h3{margin-left:3.5em}
|
||||
<h2>...no one will miss you...</h2>
|
||||
<div class="appendix">
|
||||
<h1 class="appendix">Appendix A. <a href="porting-code-to-python-3-with-2to3.html">Porting code to Python 3 with <code class="filename">2to3</code></a></h1>
|
||||
<h2><a href="#divingin">Diving in</a></h2>
|
||||
<h2><a href="#print"><code>print</code> statement</a></h2>
|
||||
<h2><a href="#ne"><> comparison</a></h2>
|
||||
<h2><a href="#has_key"><code>has_key()</code> dictionary method</a></h2>
|
||||
<h2><a href="#dict">Dictionary methods that return lists</a></h2>
|
||||
<h2><a href="#imports">Modules that have been renamed or reorganized</a></h2>
|
||||
<h3><a href="#http"><code>http</code> package</a></h3>
|
||||
<h3><a href="#urllib"><code>urllib</code> package</a></h3>
|
||||
<h3><a href="#dbm"><code>dbm</code> package</a></h3>
|
||||
<h3><a href="#xmlrpc"><code>xmlrpc</code> package</a></h3>
|
||||
<h3><a href="#othermodules">Other modules</a></h3>
|
||||
<h2><a href="#import">Relative imports within a package</a></h2>
|
||||
<h2><a href="#filter"><code>filter()</code> global function</a></h2>
|
||||
<h2><a href="#map"><code>map()</code> global function</a></h2>
|
||||
<h2><a href="#reduce"><code>reduce()</code> global function (3.1+)</a></h2>
|
||||
<h2><a href="#apply"><code>apply()</code> global function</a></h2>
|
||||
<h2><a href="#intern"><code>intern()</code> global function</a></h2>
|
||||
<h2><a href="#exec"><code>exec</code> statement</a></h2>
|
||||
<h2><a href="#execfile"><code>execfile</code> statement (3.1+)</a></h2>
|
||||
<h2><a href="#repr"><code>repr</code> literals (backticks)</a></h2>
|
||||
<h2><a href="#except"><code>try...except</code> statement</a></h2>
|
||||
<h2><a href="#raise"><code>raise</code> statement</a></h2>
|
||||
<h2><a href="#throw"><code>throw</code> statement</a></h2>
|
||||
<h2><a href="#long"><code>long</code> data type</a></h2>
|
||||
<h2><a href="#xrange"><code>xrange()</code> global function</a></h2>
|
||||
<h2><a href="#raw_input"><code>raw_input()</code> and <code>input()</code> global functions</a></h2>
|
||||
<h2><a href="#funcattrs"><code>func_*</code> function attributes</a></h2>
|
||||
<h2><a href="#xreadlines"><code>xreadlines()</code> I/O method</a></h2>
|
||||
<h2><a href="#tuple_params"><code>lambda</code> functions with multiple parameters</a></h2>
|
||||
<h2><a href="#methodattrs">Special method attributes</a></h2>
|
||||
<h2><a href="#next"><code>next()</code> iterator method</a></h2>
|
||||
<h2><a href="#nonzero"><code>__nonzero__</code> special class attribute</a></h2>
|
||||
<h2><a href="#numliterals">Number literals</a></h2>
|
||||
<h2><a href="#renames"><code>sys.maxint</code></a></h2>
|
||||
<h2><a href="#unicode"><code>unicode()</code> global function</a></h2>
|
||||
<h2><a href="#unicodeliteral">Unicode string literals</a></h2>
|
||||
<h2><a href="#callable"><code>callable()</code> global function</a></h2>
|
||||
<h2><a href="#zip"><code>zip()</code> global function</a></h2>
|
||||
<h2><a href="#standarderror"><code>StandardError()</code> exception</a></h2>
|
||||
<h2><a href="#types"><code class="filename">types</code> module constants</a></h2>
|
||||
<h2><a href="#isinstance"><code>isinstance</code> global function (3.1+)</a></h2>
|
||||
<h2><a href="#basestring"><code>basestring</code> datatype</a></h2>
|
||||
<h2><a href="#itertools"><code class="filename">itertools</code> module</a></h2>
|
||||
<h2><a href="#sys_exc"><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></a></h2>
|
||||
<h2><a href="#paren">List comprehensions over tuples</a></h2>
|
||||
<h2><a href="#getcwdu"><code>os.getcwdu()</code> function</a></h2>
|
||||
<h2><a href="#metaclass">Metaclasses</a></h2>
|
||||
<h2><a href="#set_literal"><code>set()</code> literals</a></h2>
|
||||
<h2><a href="#buffer"><code>buffer()</code> global function</a></h2>
|
||||
<h2><a href="#wscomma">Whitespace around commas</a></h2>
|
||||
<h2><a href="#idioms">Common idioms</a></h2>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#divingin">Diving in</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#print"><code>print</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicodeliteral">Unicode string literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicode"><code>unicode()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#long"><code>long</code> data type</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#ne"><> comparison</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#has_key"><code>has_key()</code> dictionary method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dict">Dictionary methods that return lists</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#imports">Modules that have been renamed or reorganized</a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#http"><code>http</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#urllib"><code>urllib</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dbm"><code>dbm</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xmlrpc"><code>xmlrpc</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#othermodules">Other modules</a>
|
||||
</ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#import">Relative imports within a package</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#next"><code>next()</code> iterator method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#filter"><code>filter()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#map"><code>map()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#reduce"><code>reduce()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#apply"><code>apply()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#intern"><code>intern()</code> global function</a>
|
||||
<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#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#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>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xreadlines"><code>xreadlines()</code> I/O method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#tuple_params"><code>lambda</code> functions with multiple parameters</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#methodattrs">Special method attributes</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#nonzero"><code>__nonzero__</code> special class attribute</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#numliterals">Octal literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#renames"><code>sys.maxint</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#callable"><code>callable()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#zip"><code>zip()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#standarderror"><code>StandardError()</code> exception</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#types"><code>types</code> module constants</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#isinstance"><code>isinstance()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#basestring"><code>basestring</code> datatype</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#itertools"><code>itertools</code> module</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#sys_exc"><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></a>
|
||||
<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)
|
||||
</ol>
|
||||
</div>
|
||||
<p class="c">This site is optimized for Lynx just because fuck you.<br>I’m told it also looks good in graphical browsers.
|
||||
<p class="c">© 2001-4, 2009 <span>ℳ</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
h1:before{counter-increment:h1;content:"Appendix A. "}
|
||||
h2:before{counter-increment:h2;content:"A." counter(h2) ". "}
|
||||
h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
|
||||
td{font-family:monospaced}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -674,7 +673,7 @@ except:
|
||||
<p>You should never use a fallback to catch <em>all</em> exceptions when importing modules (or most other times). Doing so will catch things like <code>KeyboardInterrupt</code> (if the user pressed <kbd>Ctrl-C</kbd> to interrupt the program) and can make it more difficult to debug errors.
|
||||
</blockquote>
|
||||
<h2 id="raise"><code>raise</code> statement</h2>
|
||||
<p>FIXME intro
|
||||
<p>The syntax for raising your own exceptions has changed slightly between Python 2 and Python 3.
|
||||
<p class="skip"><a href="#skipcompareraise">skip over this table</a>
|
||||
<table id="compareraise">
|
||||
<tr><th>Notes</th>
|
||||
@@ -682,19 +681,23 @@ except:
|
||||
<th>Python 3</th>
|
||||
</tr>
|
||||
<tr><th>①</th>
|
||||
<td><code>raise MyException</code></td>
|
||||
<td><i>unchanged</i></td></tr>
|
||||
<tr><th>②</th>
|
||||
<td><code>raise MyException, "error message"</code></td>
|
||||
<td><code>raise MyException("error message")</code></td></tr>
|
||||
<tr><th>②</th>
|
||||
<tr><th>③</th>
|
||||
<td><code>raise MyException, "error message", a_traceback</code></td>
|
||||
<td><code>raise MyException("error message").with_traceback(a_traceback)</code></td></tr>
|
||||
<tr><th>③</th>
|
||||
<tr><th>④</th>
|
||||
<td><code>raise "error message"</code></td>
|
||||
<td><i>unsupported</i></td></tr>
|
||||
</table>
|
||||
<ol id="skipcompareraise">
|
||||
<li>...
|
||||
<li>...
|
||||
<li>...
|
||||
<li>In the simplest form, raising an exception without a custom error message, the syntax is unchanged.
|
||||
<li>The change becomes noticeable when you want to raise an exception with a custom error message. Python 2 separated the exception class and the message with a comma; Python 3 passes the error message as a parameter.
|
||||
<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
|
||||
@@ -751,7 +754,7 @@ except:
|
||||
<li>The <code>sum()</code> function will also work with an iterator, so <code>2to3</code> makes no changes here either. Like <a href="#dict">dictionary methods that return views instead of lists</a>, this applies to <code>min()</code>, <code>max()</code>, <code>sum()</code>, <code>list()</code>, <code>tuple()</code>, <code>set()</code>, <code>sorted()</code>, <code>any()</code>, and <code>all()</code>.
|
||||
</ol>
|
||||
<h2 id="raw_input"><code>raw_input()</code> and <code>input()</code> global functions</h2>
|
||||
<p>FIXME intro
|
||||
<p>Python 2 had two global functions for asking the user for input on the command line. The first, called <code>input()</code>, expected the user to enter a Python expression (and returned the result). The second, called <code>raw_input()</code>, just returned whatever the user typed. This was wildly confusing for beginners and wildly regarded as a “wart” in the language. Python 3 excises this wart by renaming <code>raw_input()</code> to <code>input()</code>, so it works the way everyone naively expects it to work.
|
||||
<p class="skip"><a href="#skipcompareraw_input">skip over this table</a>
|
||||
<table id="compareraw_input">
|
||||
<tr><th>Notes</th>
|
||||
@@ -767,15 +770,11 @@ except:
|
||||
<tr><th>③</th>
|
||||
<td><code>input()</code></td>
|
||||
<td><code>eval(input())</code></td></tr>
|
||||
<tr><th>④</th>
|
||||
<td><code>input("prompt")</code></td>
|
||||
<td><code>eval(input("prompt"))</code></td></tr>
|
||||
</table>
|
||||
<ol id="skipcompareraw_input">
|
||||
<li>...
|
||||
<li>...
|
||||
<li>...
|
||||
<li>...
|
||||
<li>In the simplest form, <code>raw_input()</code> becomes <code>input()</code>.
|
||||
<li>In Python 2, the <code>raw_input()</code> function could take a prompt as a parameter. This has been retained in Python 3.
|
||||
<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
|
||||
|
||||
Reference in New Issue
Block a user