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.
The increase in size of the community has caused some growing pains.
Now that the early adopters are already using Python, growth must
come from luring more conservative users to the language. This is
easy enough, as Python is a very friendly language, but it does
present new challenges. For example, as a special case of
Murphy's law, anything that can go wrong during the
installation process will go wrong for someone, somewhere, and they
won't be pleased. The new Python users are often not
savvy enough to diagnose and correct problems themselves, so our
solution has been to make the installer even more bulletproof than it
already was.
The same holds for almost all aspects of the language: from the
documentation and the error messages to the
runtime's behavior in long-running servers, Python
gets more user-testing than I ever bargained for. Of course, we also
get more offers to help, so all in all, things are working out very
nicely. What this means is that we've had to change
some of our habits. You could say that the Python developer community
is losing some of its innocence: we're no longer
improving Python just for our own sake. Many hundreds of thousands of
individual Python users are affected, and an ever-growing number of
companies are using or selling software based on Python. For their
benefit, we now issue strictly backward-compatible bug-fix releases
for Python versions up to 2 years old, which are distinct from the
feature-introducing major releases every 6 to 12 months.
Let me end on a different aspect of the community: the Python
Software Foundation. After the failed experiments of the Python
Software Activity and the Python Consortium, I believe we have
finally found the proper legal form for a nonprofit organization
focused on Python. Keeping a fairly low profile, the PSF is quietly
becoming a safe haven for Python software, where no single individual
or organization can hold a monopoly on Python, and where everybody
benefits. The PSF, in turn, benefits from the sales of this book: a
portion of the royalties goes to the PSF, representing the many
Python programmers who contributed one or more recipes to the
cookbook project. Long live the Python community!
- �Guido van Rossum
- Reston, Virginia
- April 2002
|