Files
dive-into-python3/index.html
T
2009-02-05 15:04:36 -05:00

329 lines
17 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dive Into Python 3</title>
<link rel="stylesheet" type="text/css" href="dip3.css">
<link rel="shortcut icon" href="data:image/ico,">
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
<style type="text/css">
body{counter-reset:h1 -1}
h1{background:papayawhip}
h2{margin-left:1.75em}
h3{margin-left:3.5em}
.appendix h1:before{content:""}
</style>
</head>
<body>
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"><input name="q" size="31">&nbsp;<input type="submit" name="sa" value="Search"></div></form>
<p style="clear:both;margin-top:0;padding-top:1.75em"><cite>Dive Into Python 3</cite> will cover Python 3 and its differences from Python 2. Compared to the original <cite><a href="http://diveintopython.org/">Dive Into Python</a></cite>, it will be about 50% revised and 50% new material. I will publish drafts online as I go. The final version will be published on paper by Apress. The book will remain online under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a> license.
<p>There is a <a href="http://hg.diveintopython3.org/">changelog</a>, a <a rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">feed</a>, and <a href="http://www.reddit.com/search?q=%22Dive+Into+Python+3%22">discussion on Reddit</a>. The final version will be downloadable as HTML and PDF. During development, the only way to download it is to clone the Mercurial repository:
<pre><samp class="prompt">you@localhost:~$ </samp><kbd>hg clone http://hg.diveintopython3.org/ diveintopython3</kbd></pre>
<p>Below is the draft table of contents. It is <b>not finalized</b>. Only a few chapters have been written so far. The rest is just stubs and random notes to myself.
<h1>Installing Python</h1>
<h2>Python on Windows</h2>
<h2>Python on Mac OS X</h2>
<h2>Python on Linux</h2>
<h2>Python from source</h2>
<h2>The interactive shell</h2>
<h2>Summary</h2>
<h1><a href="your-first-python-program.html">Your first Python program</a></h1>
<h2>Diving in</h2>
<h2>Declaring functions</h2>
<h3>How Python's datatypes compare to other programming languages</h3>
<h2>Writing readable code</h2>
<h3>Why bother?</h3>
<h3>Docstrings</h3>
<h3>Function annotations</h3>
<h3>Style conventions</h3>
<h2>Everything is an object</h2>
<h3>The import search path</h3>
<h3>What's an object?</h3>
<h2>Indenting code</h2>
<h2>Testing modules</h2>
<h2>Summary</h2>
<h1>Native Python datatypes</h1>
<!-- "Lists and tuples and sets, oh my!" -->
<h2>Lists</h2>
<h3>Differences from Python 2</h3>
<h3>Creating new a list</h3>
<h3>Modifying a list</h3>
<h3>Searching a list</h3>
<h3>Deleting elements from a list</h3>
<h3>List operators</h3>
<h3>Looping through a list (list comprehensions)</h3>
<h3>Tuples</h3>
<h2>Dictionaries</h2>
<h3>Differences from Python 2</h3>
<h3>Creating a new dictionary</h3>
<h3>Modifying a dictionary</h3>
<h3>Deleting items from a dictionary</h3>
<h3>Looping through a dictionary (dictionary comprehensions)</h3>
<h3>Dictionary views</h3>
<h2>Sets</h2>
<h3>Differences from Python 2</h3>
<h3>Creating a new set</h3>
<h3>Modifying a set</h3>
<h3>Deleting elements from a set</h3>
<h3>Common set operations: union, intersection, and difference</h3>
<h3>Frozen sets</h3>
<h2>Numbers</h2>
<h3>Differences from Python 2</h3>
<h3>Integers</h3>
<h3>Long integers</h3>
<h3>Floating point numbers</h3>
<h3>Complex numbers</h3>
<h3>Common numerical operations</h3>
<h1></h1>
<!-- "I read part of it all the way through." -->
<h2>Iterators</h2>
<h2>Generators</h2>
<h2>Views</h2>
<h2>...</h2>
<h1>Strings</h1>
<h2>There ain't no such thing as "plain text"</h2>
<h3>A brief history of character encoding</h3>
<h3>What's a character?</h3>
<h3>How strings are stored in memory</h3>
<h3>Converting between different character encodings</h3>
<h2>Differences from Python 2</h2>
<h2>Formatting strings</h2>
<h2>What's my string?</h2>
<h2>Lists and strings</h2>
<h2>Historical note on the string module</h2>
<h2>Byte streams</h2>
<h2>Summary</h2>
<h1>The power of introspection</h1>
<h2>Diving in</h2>
<h2>Using optional and named arguments</h2>
<h3>Keyword-only arguments</h3>
<h2>Using type, str, dir, and other built-in functions</h2>
<h3>The type function</h3>
<h3>The str function</h3>
<h3>Built-in functions</h3>
<h2>Getting object references with getattr</h2>
<h3>getattr with modules</h3>
<h3>getattr as a dispatcher</h3>
<h2>Filtering lists</h2>
<h2>The peculiar nature of and and or</h2>
<h3>Using the and-or trick</h3>
<h2>Using lambda functions</h2>
<h3>Real-world lambda functions</h3>
<h2>Putting it all together</h2>
<h2>Summary</h2>
<h1>Objects and object-orientation</h1>
<h2>...major changes afoot...</h2>
<h2>...stuff about decorators...</h2>
<h2>...stuff about importing modules...</h2>
<h3>...mention why "from module import *" is only allowed at module level</h3>
<h1>Exceptions</h1>
<h2>...</h2>
<h1>Files</h1>
<h2>File objects</h2>
<h2>Reading files</h2>
<h2>Close your files... or don't</h2>
<h2>Handling I/O errors</h2>
<h2>Writing to files</h2>
<h1>Regular expressions</h1>
<h2>Diving in</h2>
<h2>Case study: street addresses</h2>
<h2>Case study: Roman numerals</h2>
<h3>Checking for thousands</h3>
<h3>Checking for hundreds</h3>
<h2>Using the {n,m} syntax</h2>
<h3>Checking for tens and ones</h3>
<h2>Verbose regular expressions</h2>
<h2>Case study: parsing phone numbers</h2>
<h2>Summary</h2>
<h1>HTML processing</h1>
<h2>Diving in</h2>
<h2>html5lib</h2>
<h3>Installing html5lib</h3>
<h3>Using html5lib</h3>
<h2>Extracting data from HTML documents</h2>
<h2>Building HTML documents</h2>
<h2>Putting it all together</h2>
<h2>Summary</h2>
<h1>XML Processing</h1>
<h2>...major changes afoot...</h2>
<h1>HTTP web services</h1>
<h2>Diving in</h2>
<h2>How not to fetch data over HTTP</h2>
<h2>Features of HTTP</h2>
<h3>User-Agent</h3>
<h3>Redirects</h3>
<h3>Last-Modified/If-Modified-Since</h3>
<h3>ETag-If-None-Match</h3>
<h3>Compression</h3>
<h2>Differences from Python 2</h2>
<h2>httplib2 (note: needs port)</h2>
<h3>Installing httplib2</h3>
<h3>Why httplib2 is better than http.client</h3>
<h2>Debugging HTTP web services</h2>
<h2>Setting the User-Agent</h2>
<h2>Handling Last-Modified and ETag</h2>
<h2>Handling redirects</h2>
<h2>Handling compressed data</h2>
<h2>Putting it all together</h2>
<h2>Summary</h2>
<h1>Unit testing</h1>
<h2>Introduction to Roman numerals</h2>
<h2>Diving in</h2>
<h2>Introducing romantest.py</h2>
<h2>Testing for success</h2>
<h2>Testing for failure</h2>
<h2>Testing for sanity</h2>
<h1>Test-first programming</h1>
<h2>roman.py, stage 1</h2>
<h2>roman.py, stage 2</h2>
<h2>roman.py, stage 3</h2>
<h2>roman.py, stage 4</h2>
<h2>roman.py, stage 5</h2>
<h1>Refactoring your code</h1>
<h2>Handling bugs</h2>
<h2>Handling changing requirements</h2>
<h2>The art of refactoring</h2>
<h2>Postscript</h2>
<h2>Summary</h2>
<h1>Dynamic functions</h1>
<h2>Diving in</h2>
<h2>plural.py, stage 1</h2>
<h2>plural.py, stage 2</h2>
<h2>plural.py, stage 3</h2>
<h2>plural.py, stage 4</h2>
<h2>plural.py, stage 5</h2>
<h2>plural.py, stage 6</h2>
<h2>Summary</h2>
<h1>Metaclasses</h1>
<h2>...once I figure out WTF metaclasses are...</h2>
<h1>Performance tuning</h1>
<h2>Diving in</h2>
<h2>Using the timeit module</h2>
<h2>Optimizing regular expressions</h2>
<h2>Optimizing dictionary lookups</h2>
<h2>Optimizing list operations</h2>
<h2>Optimizing string manipulation</h2>
<h2>Summary</h2>
<h1><a href="case-study-porting-chardet-to-python-3.html">Case study: porting <code>chardet</code> to Python 3</a></h1>
<h2><a href="#faq">Introducing <code class="filename">chardet</code>: a mini-FAQ</a></h2>
<h3><a href="#faq.what">What is character encoding auto-detection?</a></h3>
<h3><a href="#faq.impossible">Isn't that impossible?</a></h3>
<h3><a href="#faq.who">Who wrote this detection algorithm?</a></h3>
<h3><a href="#faq.yippie">Yippie! Screw the standards, I'll just auto-detect everything!</a></h3>
<h3><a href="#faq.why">Why bother with auto-detection if it's slow, inaccurate, and non-standard?</a></h3>
<h2><a href="#divingin">Diving in</a></h2>
<h3><a href="#how.bom"><code>UTF-n</code> with a <abbr title="Byte Order Mark">BOM</abbr></a></h3>
<h3><a href="#how.esc">Escaped encodings</a></h3>
<h3><a href="#how.mb">Multi-byte encodings</a></h3>
<h3><a href="#how.sb">Single-byte encodings</a></h3>
<h3><a href="#how.windows1252"><code>windows-1252</code></a></h3>
<h2><a href="#running2to3">Running <code class="filename">2to3</code></a></h2>
<h2><a href="#manual">Fixing what <code class="filename">2to3</code> can't</a></h2>
<h3><a href="#falseisinvalidsyntax"><code>False</code> is invalid syntax</a></h3>
<h3><a href="#nomodulenamedconstants">No module named <code class="filename">constants</code></a></h3>
<h3><a href="#namefileisnotdefined">Name '<var>file</var>' is not defined</a></h3>
<h3><a href="#cantuseastringpattern">Can't use a string pattern on a bytes-like object</a></h3>
<h3><a href="#cantconvertbytesobject">Can't convert '<code>bytes</code>' object to <code>str</code> implicitly</a></h3>
<h1>Packaging Python libraries</h1>
<!-- http://pypi.python.org/pypi -->
<h2>A brief history of packaging (and why it's harder than you think)</h2>
<h2>setuptools</h2>
<h2>distutils</h2>
<h2>Eggs</h2>
<h2>pip</h2>
<h2>Platform-specific packaging</h2>
<h3>Packaging by Linux distributions</h3>
<h3>Py2exe</h3>
<h1>Creating graphics with the Python Imaging Library</h1>
<h2>...<a href="http://www.reddit.com/r/Python/comments/7sj39/dive_into_python_3/c07b3cq">will likely get ported in time</a>...</h2>
<h1>Where to go from here</h1>
<p>Tentative because most of these have not been ported to Python 3 yet.
<h2>WSGI</h2>
<h2>Django</h2>
<h2>Pylons</h2>
<h2>TurboGears</h2>
<h2>AppEngine</h2>
<h2>IronPython</h2>
<h2>Jython</h2>
<h2>PyPy</h2>
<h2>Stackless Python</h2>
<h1><del>Scripts and streams</del></h1>
<h2>...will be folded into other chapters...</h2>
<h1><del>Functional programming</del></h1>
<h2>...bits and pieces will be folded into other chapters...</h2>
<h1><del>SOAP web services</del></h1>
<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>
<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">&lt;> 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#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> 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>
<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#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.
<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>
<!--
As I write this, the year is 2009, and the internet is STILL a battleground of so-called "intellectual property" disputes. Some people would have you believe that without proper financial incentives, music, literature, and software would disappear. After all, who would make music if they can't make money on it? Who would write? Who would program?
I know the answer. The answer is that musicians will make music, not because they can make money, but because musicians are the people who can't not make music. Writers will write because they can't not write. Most of the people you think of as artists are really just showmen. They collect a paycheck and go home at 5 o'clock. That's not art, that's commerce.
I've been programming since 1983 and releasing my code under Free Software licenses since 1993. I've been writing and publishing under Free Content licenses since 2000. I can't imagine not doing this. If you can imagine yourself not doing what you're doing, do something else. Do whatever it is you can't not do.
-->
</body>
</html>