<linkrel=stylesheetmedia='only screen and (max-device-width: 480px)'href=mobile.css>
<formaction=http://www.google.com/cse><div><inputtype=hiddenname=cxvalue=014021643941856155761:l5eihuescdw><inputtype=hiddenname=ievalue=UTF-8> <inputtype=searchname=qsize=25placeholder="powered by Google™"> <inputtype=submitname=savalue=Search></div></form>
<p>You are here: <ahref=index.html>Home</a><spanclass=u>‣</span><ahref=table-of-contents.html#whats-new>Dive Into Python 3</a><spanclass=u>‣</span>
<h1>What’s New In “Dive Into Python 3”</h1>
<p><spanclass=u>❝</span> Isn’t this where we came in? <spanclass=u>❞</span><br>— Pink Floyd, The Wall
<h2id=divingin><i>a.k.a.</i>“the minus level”</h2>
<pclass=f>Are you already a Python programmer? Did you read the original “<ahref=http://diveintopython.org/>Dive Into Python</a>”? Did you buy it on paper? (If so, thanks!) Are you ready to take the plunge into Python 3? … If so, read on. (If none of that is true, you’d be better off <ahref=installing-python.html>starting at the beginning</a>.)
<p>Python 3 comes with a script called <code>2to3</code>. Learn it. Love it. Use it. <ahref=porting-code-to-python-3-with-2to3.html>Porting Code to Python 3 with <code>2to3</code></a> is a reference of all the things that the <code>2to3</code> tool can fix automatically. Since a lot of those things are syntax changes, it’s a good starting point to learn about a lot of the syntax changes in Python 3. (<code>print</code> is now a function, <code>`x`</code> doesn’t work, <iclass=baa>&</i>c.)
<p><ahref=case-study-porting-chardet-to-python-3.html>Case Study: Porting <code>chardet</code> to Python 3</a> documents my (ultimately successful) effort to port a non-trivial library from Python 2 to Python 3. It may help you; it may not. There’s a fairly steep learning curve, since you need to kind of understand the library first, so you can understand why it broke and how I fixed it. A lot of the breakage centers around strings. Speaking of which…
<p>Strings. Whew. Where to start. Python 2 had “strings” and “Unicode strings.” Python 3 has “bytes” and “strings.” That is, all strings are now Unicode strings, and if you want to deal with a bag of bytes, you use the new <code>bytes</code> type. Python 3 will <em>never</em> implicitly convert between strings and bytes, so if you’re not sure which one you have at any given moment, your code will almost certainly break. Read <ahref=strings.html>the Strings chapter</a> for more details.
<p>Bytes vs. strings comes up again and again throughout the book.
<li>In <ahref=files.html>Files</a>, you’ll learn the difference between reading files in “binary” and “text” mode. Reading (and writing!) files in text mode requires an <code>encoding</code> parameter. Some text file methods count characters, but other methods count bytes. If your code assumes that one character == one byte, it <em>will</em> break on multi-byte characters.
<li>In <ahref=http-web-services.html><abbr>HTTP</abbr> Web Services</a>, the <code>httplib2</code> module fetches headers and data over <abbr>HTTP</abbr>. <abbr>HTTP</abbr> headers are returned as strings, but the <abbr>HTTP</abbr> body is returned as bytes.
<li>In <ahref=serializing.html>Serializing Python Objects</a>, you’ll learn why the <code>pickle</code> module in Python 3 defines a new data format that is backwardly incompatible with Python 2. (Hint: it’s because of bytes and strings.) Also, Python 3 supports serializing objects to and from <abbr>JSON</abbr>, which doesn’t even have a <code>bytes</code> type. I’ll show you how to hack around that.
<li>In <ahref=case-study-porting-chardet-to-python-3.html>Case study: porting <code>chardet</code> to Python 3</a>, it’s just a bloody mess of bytes and strings everywhere.
<p>Even if you don’t care about Unicode (oh but you will), you’ll want to read about <ahref=strings.html#formatting-strings>string formatting in Python 3</a>, which is completely different from Python 2.
<p>Iterators are everywhere in Python 3, and I understand them a lot better than I did five years ago when I wrote “Dive Into Python”. You need to understand them too, because lots of functions that used to return lists in Python 2 will now return iterators in Python 3. At a minimum, you should read <ahref=iterators.html#a-fibonacci-iterator>the second half of the Iterators chapter</a> and <ahref=advanced-iterators.html#generator-expressions>the second half of the Advanced Iterators chapter</a>.
<p>By popular request, I’ve added an appendix on <ahref=special-method-names.html>Special Method Names</a>, which is kind of like <ahref=http://www.python.org/doc/3.1/reference/datamodel.html#special-method-names>the Python docs “Data Model” chapter</a> but with more snark.
<p>When I was writing “Dive Into Python”, all of the available XML libraries sucked. Then Fredrik Lundh wrote <ahref=http://effbot.org/zone/element-index.htm>ElementTree</a>, which doesn’t suck at all. The Python gods wisely <ahref=http://docs.python.org/3.1/library/xml.etree.elementtree.html>incorporated ElementTree into the standard library</a>, and now it forms the basis for <ahref=xml.html>my new XML chapter</a>. The old ways of parsing XML are still around, but you should avoid them, because they suck!
<p>Also new in Python — not in the language but in the community — is the emergence of code repositories like <ahref=http://pypi.python.org/>The Python Package Index</a> (PyPI). Python comes with utilities to package your code in standard formats and distribute those packages on PyPI. Read <ahref=packaging.html>Packaging Python Libraries</a> for details.