Book Review: Dive Into Python 3

I received a review copy of Mark Pilgrim’s updated Dive Into Python 3_ back in early November, but with the various holidays and end-of-year activities I didn’t have a chance to read it until the past week or so. I’m glad I waited until I had the time to sit down and look over it carefully, because there is a lot of good material here!

Disclaimer: The review copy was free.

Quick Review

Inspired by Gene Ambaum and Bill Barnes’ review short-cuts, here’s my review for the impatient reader:

Why I picked it up: Mark’s work has been the standard go-to guide for learning Python for years now. I wanted to see what the new version had to offer.

Why I finished it: No fluffy, contrived examples. Real-world code and concise, clear discussion. And there’s no attempt to avoid challenging topics.

I’d give it to: Beginner to intermediate programmers who want to learn Python or improve their existing Python skills and experienced programmers who want to learn about the new features of Python 3.

Completely Updated

Many of you may be familiar with DiveIntoPython.org, one of the earliest major works published under an open license. The new edition of the book has been updated to cover Python 3.1, including new examples and entire new sections on language features not present in earlier versions of the language. Starting with comprehensive setup instructions to get Python installed, the introduction leaves no excuses for not following along with the coding examples as you read. It even includes advice for picking a text editor, in case you don’t have one already.

Dives Right In

True to the title, the first code sample in Dive Into Python is a complete program to convert integers to human-readable approximations of different units. It uses I/O, dictionaries, functions (with docstrings), new-style string formatting, and exceptions. On page 1.

Books about programming usually start with a bunch of boring chapters about fundamentals and eventually work up to building something useful. Let’s skip all that.

The more traditional discussion of integers, floats, strings, and data structures is included early in the book, but that first example grabs the attention better than the beginnings of many introductory texts.

Just the Facts

The explanation of that first program avoids fluff, staying on point and addressing the reader as an intelligent learner. Striking that balance between explaining the minutia of an example and letting the reader discover the answer for themselves can be difficult, but Mark finds it every time. This pattern is repeated throughout the book, with each code sample being introduced, then examined in detail. Some of the more important or interesting parts of the code are called out for special discussion, as needed. And a few times sample code from earlier in the book is reused in a later section. That code-reuse reinforces the earlier point, while building on it to make the new point.

Real-world Advice

Throughout the book technical details about using Python (the language and libraries) are mixed with solid developer advice on topics like writing readable code, choosing tools, and getting the most out of techniques such as testing. Chapter 14 covers HTTP web services, and doesn’t stop with the basics of using httplib. In addition to talking about third-party packages like httplib2, the chapter also stresses the importance of creating well-behaved web clients and shows techniques for doing so.

Beyond the Basics

The book also includes chapters on advanced topics such as porting from Python 2 to Python 3 and packaging a library or application for distribution. The porting chapter is actually a case study of porting a real Python 2 library, chardet. It goes through the process of running 2to3, and then works through examples of the conversion steps that have to be done by hand. There’s a separate appendix with even more detail about the types of automated translations 2to3 can make.

The more exotic your installation process is, the more exotic your bug reports will be.

The packaging chapter stresses the need to follow standards and conventions to make distributing your code easier – both for yourself and and for your users. It covers the details of using distutils and PyPI, licensing, and building source and binary distributions.

Conclusions

I can recommend Dive Into Python for any beginner or intermediate programmer with a desire to pick up Python from scratch, or improve their existing Python skills. More experienced programmers will find the first few chapters fairly light reading, but the chapters on HTTP, XML, and other advanced topics in the rest of the book helpful.

The porting and packaging chapters were of special interest to me. This is starting to look like the year we should all be porting our libraries to Python 3, so I imagine I’ll be re-reading the material on 2to3 a few times over the next few weeks.