mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
43 lines
4.8 KiB
HTML
43 lines
4.8 KiB
HTML
<!DOCTYPE html>
|
|
<head>
|
|
<meta charset=utf-8>
|
|
<title>What's New In Dive into Python 3</title>
|
|
<!--[if IE]><script src=j/html5.js></script><![endif]-->
|
|
<link rel=stylesheet href=dip3.css>
|
|
<style>
|
|
body{counter-reset:h1 -1}
|
|
h3:before{content:''}
|
|
</style>
|
|
<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>
|
|
<link rel=stylesheet media=print href=print.css>
|
|
<meta name=viewport content='initial-scale=1.0'>
|
|
</head>
|
|
<form action=http://www.google.com/cse><div><input type=hidden name=cx value=014021643941856155761:l5eihuescdw><input type=hidden name=ie value=UTF-8> <input name=q size=25> <input type=submit name=sa value=Search></div></form>
|
|
<p>You are here: <a href=index.html>Home</a> <span class=u>‣</span> <a href=table-of-contents.html#whats-new>Dive Into Python 3</a> <span class=u>‣</span>
|
|
<h1>What’s New In “Dive Into Python 3”</h1>
|
|
<blockquote class=q>
|
|
<p><span class=u>❝</span> Isn’t this where we came in? <span class=u>❞</span><br>— Pink Floyd, The Wall
|
|
</blockquote>
|
|
<p id=toc>
|
|
<h2 id=divingin><i>a.k.a.</i> “the minus level”</h2>
|
|
|
|
<p class=f>You read the original “<a href=http://diveintopython.org/>Dive Into Python</a>” and maybe even bought it on paper. (Thanks!) You already know Python 2 pretty well. You’re ready to take the plunge into Python 3. … If all of that is true, read on. (If none of that is true, you’d be better off <a href=your-first-python-program.html>starting at the beginning</a>.)
|
|
|
|
<p>Python 3 comes with a script called <code>2to3</code>. Learn it. Love it. Use it. <a href=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, <i class=baa>&</i>c.)
|
|
|
|
<p><a href=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. Oh, and Python 3 will never implicitly convert between strings and bytes, so if you’re not sure which one you have, your code will almost certainly break. Read <a href=strings.html>the Strings chapter</a> for more details.
|
|
|
|
<p>Even if you don’t care about Unicode, you’ll want to read about <a href=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 <a href=iterators.html#a-fibonacci-iterator>the second half of the Iterators chapter</a> and <a href=advanced-iterators.html#generator-expressions>the second half of the Advanced Iterators chapter</a>.
|
|
|
|
<p>By popular request, I’ve added an appendix on <a href=special-method-names.html>Special Method Names</a>, which is kind of like <a href=http://www.python.org/doc/3.0/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 <a href=http://effbot.org/zone/element-index.htm>ElementTree</a>, which doesn’t suck at all. The Python gods wisely <a href=http://docs.python.org/3.0/library/xml.etree.elementtree.html>incorporated ElementTree into the standard library</a>, and now it forms the basis for <a href=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>That’s it for now; the book’s not finished yet! The file I/O subsystem is totally different now; I hope to write about that soon.
|
|
|
|
<p class=c>© 2001–9 <a href=about.html>Mark Pilgrim</a>
|