CHAPTER -1. WHAT’S NEW IN “DIVE INTO PYTHON
3”
❝ Isn’t this where we came in? ❞
— Pink Floyd, The Wall
-1.1. A.K.A. “THE MINUS LEVEL”
You read the original “Dive Into Python” 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 starting at the beginning.)
Python 3 comes with a script called 2to3. Learn it. Love it. Use it. Porting Code to Python 3 with 2to3 is a
reference of all the things that the 2to3 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. (print is now a function, `x`
doesn’t work, &c.)
Case Study: Porting chardet to Python 3 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…
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 bytes type. Python 3 will never 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 the Strings chapter for more details.
Bytes vs. strings comes up again and again throughout the book.
1
评论30
最新资源