58
• In
Files, you’lllearnthe difference betweenreading filesin“binary” and“text” mode.Reading (andwriting!)
filesintext mode requires an
encoding
parameter. Some text filemethodscountcharacters, butother
methods count bytes.Ifyour codeassumes thatone character==onebyte, itwillbreakonmulti-byte
characters.
• In
HTTP
WebServices,the
httplib2
module fetches headersanddata over
HTTP
.
HTTP
headersare
returned as strings,but the
HTTP
bodyisreturnedasbytes.
• In
Serializing PythonObjects, you’ll learnwhythe
pickle
moduleinPython3defines a new data format that
isbackwardly incompatible with Python2.(Hint: it’sbecause ofbytes and strings.) Also
JSON
,whichdoesn’t
support the
bytes
typeat all.I’ll showyouhowto hack aroundthat.
• In
Case study:porting
chardet
to Python3, it’sjusta bloodymessofbytesandstringseverywhere.
Evenifyoudon’tcare aboutUnicode(oh butyouwill), you’ll want to readabout
string formattinginPython
3, whichiscompletelydifferentfromPython2.
Iteratorsare everywhere inPython 3,andIunderstandthema lot betterthanIdidfiveyears ago whenI
wrote “Dive Into Python”. Youneedto understand themtoo, because lotsoffunctionsthatusedto return
lists inPython2will nowreturniteratorsinPython3. At a minimum,youshouldread
the second halfof
the Iteratorschapterand
the secondhalfoftheAdvancedIterators chapter.
Bypopular request,I’ve added anappendixon
SpecialMethodNames, whichis kindoflike
thePythondocs
“Data Model” chapter butwithmore snark.
WhenIwaswriting “Dive Into Python”, all ofthe available XML librariessucked.ThenFredrik Lundhwrote
ElementTree,whichdoesn’tsuck atall. The Pythongodswisely
incorporated ElementTreeinto thestandard
library,andnowitformsthe basisfor
mynew XML chapter. Theoldwaysofparsing XML are still around,
butyoushouldavoidthem,because theysuck!
Also newinPython—notinthe language butinthecommunity—istheemergence of coderepositories
like
The PythonPackage Index(PyPI).Pythoncomes withutilities to package your code instandardformats
anddistribute those packagesonPyPI. Read
Packaging PythonLibrariesfor details.
2