Foreword
Forget the jokes about tasty snake dishes, here's the Python Cookbook! Python's famous comedian
namesakes would have known exactly what to do with this title: recipes for crunchy frog, spring
surprise, and, of course, blancmange (or was that a tennis-playing alien?). The not-quite-so-famous-
yet Python programming community has filled in the details a little differently: we like to have fun
here as much as the next person, but we're not into killing halibuts, especially not if their first name is
Eric.
So what exactly is a Python cookbook? It's a collection of recipes for Python programmers,
contributed by Python community members. The original contributions were made through a web site
set up by ActiveState, from which a selection was made by editors Alex Martelli and David Ascher.
Other Python luminaries such as Fredrik Lundh, Paul Dubois, and Tim Peters were asked to write
chapter introductions.
Few cookbooks teach how to cook, and this one is no exception: we assume that you're familiar with
programming in Python. But most of these recipes don't require that you be an expert programmer,
either, nor an expert in Python (though we've sprinkled a few hard ones throughout just to give the
gurus something to watch for). And while these recipes don't teach Python programming basics, most
were selected because they teach something—for example, performance tips, advanced techniques,
explanations of dark corners of the language, warnings about common pitfalls, and even suggestions
that seem to go against accepted wisdom.
Most recipes are short enough for the attention span of the average Python programmer. For easy
access, they are grouped into chapters, which contain either recipes for a specific application area,
such as network programming or XML, or are about specific programming techniques, such as
searching and sorting or object-oriented programming. While there's some logical progression among
the chapters and among the recipes in a chapter, we expect that most readers will sample the recipes at
random or based on the job at hand (just as you would choose a food recipe based upon your appetite
or the contents of your refrigerator).
All in all, the breadth and depth of this collection are impressive. This is a testimony to Python's wide
range of application areas, but also to its user community. When I created the first version of Python,
more than 12 years ago now, all I wanted was a language that would let me write system-
administration scripts in less time. (Oh, and I wanted it to be elegant, too.) I never could have guessed
most of the application areas where Python is currently the language of choice for many—and that's
not just because the World Wide Web hadn't been invented yet. In many areas, code written by
generous Python users is as important as Python's standard library: think of numeric algorithms,
databases, and user interfaces, in which the number of third-party choices dwarfs Python's standard-
library offerings, despite the language's reputation that it comes with "batteries included."
Python is an evolving language. This cookbook offers some recipes that work only with the latest
Python version, and a few that have been made obsolete by recent Python versions. Don't think this
means that Python has built-in obsolescence! Usually, these obsolete recipes work fine, and the code
that uses them will continue to work in future Python versions. It's just that when you're irked by a
roundabout way of expressing a particular idea in code, there's often a better way available in a newer
Python version, and we'd like you to know about it. On the other hand, it's sometimes useful to know
how to write code that works for several Python versions at once, without explicitly checking version
numbers all the time. Some recipes touch upon this topic, as well.