later in Part IV, because I believe it’s important to be comfortable using an ABC before
writing your own.
This approach has a few advantages. First, knowing what is ready to use can save you
from reinventing the wheel. We use existing collection classes more often than we im‐
plement our own, and we can give more attention to the advanced usage of available
tools by deferring the discussion on how to create new ones. We are also more likely to
inherit from existing ABCs than to create a new ABC from scratch. And finally, I believe
it is easier to understand the abstractions after you’ve seen them in action.
The downside of this strategy are the forward references scattered throughout the
chapters. I hope these will be easier to tolerate now that you know why I chose this path.
Here are the main topics in each part of the book:
Part I
A single chapter about the Python data model explaining how the special methods
(e.g., __repr__) are the key to the consistent behavior of objects of all types—in a
language that is admired for its consistency. Understanding various facets of the
data model is the subject of most of the rest of the book, but Chapter 1 provides a
high-level overview.
Part II
The chapters in this part cover the use of collection types: sequences, mappings,
and sets, as well as the str versus bytes split—the cause of much celebration among
Python 3 users and much pain for Python 2 users who have not yet migrated their
code bases. The main goals are to recall what is already available and to explain
some behavior that is sometimes surprising, like the reordering of dict keys when
we are not looking, or the caveats of locale-dependent Unicode string sorting. To
achieve these goals, the coverage is sometimes high level and wide (e.g., when many
variations of sequences and mappings are presented) and sometimes deep (e.g.,
when we dive into the hash tables underneath the dict and set types).
Part III
Here we talk about functions as first-class objects in the language: what that means,
how it affects some popular design patterns, and how to implement function dec‐
orators by leveraging closures. Also covered here is the general concept of callables
in Python, function attributes, introspection, parameter annotations, and the new
nonlocal declaration in Python 3.
Part IV
Now the focus is on building classes. In Part II, the class declaration appears in
few examples; Part IV presents many classes. Like any object-oriented (OO) lan‐
guage, Python has its particular set of features that may or may not be present in
the language in which you and I learned class-based programming. The chapters
explain how references work, what mutability really means, the lifecycle of instan‐
Preface | xvii