Copyright © 2010 Mark Lutz
O’Reilly books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles
(
http://my.safaribooksonline.com
).
For more information, contact our corporate/institutional sales
department: (800) 998-9938 or
[email protected]
.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo
are registered trademarks of O’Reilly Media, Inc.
Programming
Python
, the image of an African rock python, and related trade
dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to
distinguish their products are claimed as trademarks. Where those
designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial
caps.
While every precaution has been taken in the preparation of this
book, the publisher and author assume no responsibility for errors or
omissions, or for damages resulting from the use of the information
contained herein.
Supplemental files and examples for this book can be found at
http://examples.oreilly.com/9780596158118/
. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, we’ve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to
[email protected]
.
This book explores ways to apply the Python programming language in
common application domains and realistically scaled tasks. It’s about what
you can
do
with the language once you’ve mastered its
fundamentals.
This book assumes you are relatively new to each of the application
domains it
covers—
GUIs, the
Internet, databases, systems programming, and so on—and presents each from
the ground up, in
tutorial
fashion. Along the way, it
focuses on commonly used tools and libraries, rather than language
fundamentals. The net result is a resource that provides readers with an
in-depth understanding of Python’s roles in practical, real-world
programming work.
As a subtheme, this book also explores Python’s relevance as a
software development
tool—a role that many would
classify as well beyond those typically associated with “scripting.” In
fact, many of this book’s examples are scaled specifically for this
purpose; among these, we’ll incrementally develop email clients that top
out at thousands of lines of code. Programming at this full scale will
always be challenging work, but we’ll find that it’s also substantially
quicker and easier when done with Python.
This Fourth Edition has been updated to present the language,
libraries, and practice of Python 3.X. Specifically, its examples use
Python 3.1—the most recent version of Python at the time of writing—and
its major examples were tested successfully under the third alpha release
of Python 3.2 just prior to publication, but they reflect the version of
the language common to the entire 3.X line. This edition has also been
reorganized in ways that both streamline some of its former material and
allow for coverage of newly emerged tools and topics.
Because this edition’s readership will include both newcomers as
well as prior edition veterans, I want to use this Preface to expand on
this book’s purpose and scope before we jump into code.
This book is a tutorial introduction to using Python in common
application domains and tasks. It teaches how to apply Python for system
administration, GUIs, and the Web, and explores its roles in networking,
databases, frontend scripting layers, text processing, and more. Although
the Python language is used along the way, this book’s focus is on
application
to real-world tasks instead of language
fundamentals.
Because of its scope, this book is designed to work best as the
second of a two-volume set, and to be supplemented by a third. Most
importantly, this book is an applications programming follow-up to the
core language book
Learning
Python
, whose subjects are officially prerequisite
material here. Here’s how the three books are related:
Learning
Python
covers the fundamentals of Python
programming in depth. It focuses on the core Python language, and
its topics are prerequisite to this book.
Programming
Python
, this book, covers the application of
Python to real-world programming tasks. It focuses on libraries and
tools, and it assumes you already know Python fundamentals.
Python Pocket
Reference
provides a quick reference to details
not listed exhaustively here. It doesn’t teach much, but it allows
you to look up details fast.
In some sense, this book is to application programming what
Learning
Python
is to the core language—a gradual tutorial,
which makes almost no assumptions about your background and presents
each topic from the ground up. By studying this book’s coverage of Web
basics, for example, you’ll be equipped to build simple websites, and
you will be able to make sense of more advanced frameworks and tools as
your needs evolve. GUIs are similarly taught incrementally, from basic
to advanced.
In addition, this book is designed to be supplemented by the
quick-reference book
Python Pocket Reference
, which
provides the small details finessed here and serves as a resource for
looking up the fine points. That book is reference only, and is largely
void of both examples and narrative, but it serves to augment and
complement both
Learning Python
’s fundamentals and
Programming Python
’s applications. Because its
current Fourth Edition gives both Python 2.X and 3.X versions of the
tools it covers, that book also serves as a resource for readers
transitioning between the two Python lines (more on this in a
moment).
[
1
]
Because of the scopes carved out by the related books I just
mentioned, this book’s scope follows two explicit constraints:
It does not cover Python language fundamentals
It is not intended as a language reference
The former of these constraints reflects the fact that core
language topics are the exclusive domain of
Learning
Python
, and I encourage you to consult that book
before tackling this one if you are completely new to the Python
language, as its topics are assumed here. Some language techniques are
shown by example in this book too, of course, and the larger examples
here illustrate how core concepts come together into realistic programs.
OOP, for example, is often best sampled in the context of the larger
programs we’ll write here. Officially, though, this book assumes you
already know enough Python fundamentals to understand its example code.
Our focus here is mostly on libraries and tools; please see other
resources if the basic code we’ll use in that role is unclear.
The latter of the two constraints listed above reflects what has
been a common misconception about this book over the years (indeed, this
book might have been better titled
Applying Python
had we been more clairvoyant in 1995). I want to make this as clear as I
can: this is
not
a reference book. It is a
tutorial
. Although you can hunt for some details
using the index and table of contents, this book is not designed for
that purpose. Instead,
Python Pocket
Reference
provides the sort of quick reference to
details that you’ll find useful once you start writing nontrivial code
on your own. There are other reference-focused resources available,
including other books and Python’s own reference manuals set. Here, the
goal is a gradual tutorial that teaches you how to apply Python to
common tasks but does not document minute details exhaustively.
[
1
]
Disclosure: I am the author of all three books mentioned in
this section, which affords me the luxury of tightly controlling
their scopes in order to avoid overlap. It also means that as an
author, I try to avoid commenting on the many other Python books
available, some of which are very good and may cover topics not
addressed in any of my own books. Please see the Web for other
Python resources. All three of my books reflect my 13 years on the
Python training trail and stem from the original
Programming Python
written back in 1995
If this is the first edition of this book you’ve seen, you’re
probably less interested in recent changes, and you should feel free to
skip ahead past this section. For readers of prior editions, though, this
Fourth Edition of this book has changed in three important ways:
It’s been updated to cover Python 3.X (only).
It’s been slimmed down to sharpen its focus and make room for
new topics.
It’s been updated for newly emerged topics and tools in the
Python world.
The first of these is probably the most significant—this edition
employs the Python 3.X language, its version of the standard library, and
the common practice of its users. To better explain how this and the other
two changes take shape in this edition, though, I need to fill in a few
more details.
Because the prior versions of this book were widely read, here is
a quick rundown of some of the most prominent specific changes in this
edition:
The prior edition of this book was also a 1600-page volume,
which didn’t allow much room for covering new Python topics
(Python 3.X’s Unicode orientation alone implies much new
material). Luckily, recent changes in the Python world have
allowed us to pare down some less critical existing material this
time around, in order to free up room for new coverage.
Depth was not sacrificed in the process, of course, and this
is still just as substantial a book as before. In general, though,
avoiding new growth was a primary goal of this update; many of the
other specific changes and removals I'll mention below were made,
in part, to help accommodate new topics.
This book’s examples and narrative have been updated to
reflect and use the 3.X version of Python. Python 2.X is no longer
supported here, except where 3.X and 2.X Pythons overlap. Although
the overlap is large enough to make this of use to 2.X readers
too, this is now officially a 3.X-only text.
This turns out to be a major factor behind the lack of
growth in this edition. By restricting our scope to Python 3.X—the
incompatible successor to the Python 2.X line, and considered to
be Python’s future—we were able to avoid doubling the coverage
size in places where the two Python lines differ. This version
limit is especially important in a book like this that is largely
about more advanced examples, which can be listed in only one
version’s style.
For readers who still straddle the 2.X and 3.X worlds, I’ll
say more about Python 3.X changes later in this Preface. Probably
the most significant 3.X-related change described there is the new
Internationalization support in PyEdit and PyMailGUI; though 2.X
had Unicode too, its new prominence in 3.X almost forces such
systems to rethink their former ASCII-only ways.
Since the prior edition, a variety of new libraries and
tools have either come online or risen in popularity, and they get
new mention here. This includes new standard library tools such assubprocess
(in Chapters
2
and
3
) andmultiprocessing
(in
Chapter 5
), as well as new third-party
web frameworks and ORM database toolkits. Most of these are not
covered extensively (many popular third-party extensions are
complex systems in their own right and are best covered by
dedicated books), but they are at the least introduced in summary
form here.
For example, Python 3.1’s newtkinter.ttk
Tk themed widget set shows
up in
Chapter 7
now, but only
briefly; as a rule, this edition prefers to mention such
extensions in passing, rather than attempting to show you code
without adequate explanation.
I’ve removed all the instructions for using and running
program examples. Instead, please consult theREADME
file in the examples distribution
for example usage details. Moreover, most of the original
acknowledgments are gone here because they are redundant with
those in
Learning
Python
; since that book is now considered a
prerequisite, duplication of material here is unwarranted. A
description of book contents was also deleted; please see the
table of contents for a preview of this book’s structure.
I’ve removed the prior edition’s “managerial summary”
chapter which introduced Python’s strong points, prominent users,
philosophies, and so on. Proselytizing does play an important role
in a field that sometimes asks the “why” questions less often than
it should. Indeed, if advocacy had not been part of the Python
experience, we’d probably all be using Perl or shell languages
today!
However, this chapter has now grown completely redundant
with a similar chapter in
Learning
Python
. Since that book is a precursor to this one, I
opted to not devote space to restating “Pythonista” propaganda
here (fun as it may be). Instead, this book assumes you already
know why Python is worth using, and we jump right into applying it
here.
This book’s conclusion comes from the first edition, and it
is now 15 years old. Naturally, some of it reflects the Python
mindset from that period more than that of today. For example, its
focus on Python’s role in hybrid applications seemed more
important in 1995 than in 2010; in today’s much larger Python
world, most Python users never deal with linked-in C code at
all.
In prior editions, I added postscripts for each edition to
elaborate on and update the ideas presented in the book’s
conclusion. These postscripts are gone now, replaced by a short
note at the start of the conclusion. I opted to keep the
conclusion itself, though, because it’s still relevant to many
readers and bears some historic value. Well, that, plus the
jokes…
For reasons similar to those of the prior two points, the
accumulated forewords from the prior three editions were also
dropped this time around. You can read all about Python creator
Guido van Rossum’s historical rationale for Python’s evolution in
numerous places on the Web, if you are so inclined. If you are
interested in how Python has changed technically over the years,
see also the “What’s New” documents that are part of the Python
standard manuals set (available at
http://www.python.org/doc
, and installed alongside
Python on Windows and other
platforms
).
I’ve reduced the C extending and embedding part’s material
to one shorter chapter at the end of the tools part, which briefly
introduces the core concepts in this domain. Only a fraction of
Python users must care about linking in C libraries today, and
those who do already have the skills required to read the larger
and more compete example of integration present in the source code
of Python itself. There is still enough to hint at possibilities
here, but vast amounts of C code have been cut, in deference to
the better examples you’ll find in Python’s own code.
The former two larger system examples chapters have been
merged into one shorter one, with new or greatly rewritten
examples. In fact, this part (
Part II
) was probably overhauled the most
of any part in the book. It incorporates new tools such assubprocess
andmultiprocessing
, introduces sockets
earlier, and removes dated topics and examples still lingering
from prior editions. Frankly, a few of the file-oriented examples
here dated back to the 1990s, and were overdue for a general
refresh. The initial chapter in this part was also split into two
to make its material easier to read (shell context, including
streams, gets its own chapter now), and a few large program
listings here (including the auto-configuring launcher scripts)
are now external suggested reading.
Along the same lines, two of the larger GUI examples in the
prior edition,
PyTree
and
PyForm
, have been removed. Instead, their
updated code is available in the book’s examples distribution
package, as suggested supplemental reading. You’ll still find many
larger examples covered and listed in this edition—including both
GUI- and Web-based renderings of full-featured email clients,
along with image viewers, calculators, clocks, Unicode-aware text
editors, drawing programs, regression test scripts, and more.
However, because the code of the examples removed doesn’t add much
to what is already covered, and because they were already largely
self-study examples anyhow, I’ve made them optional and external
to the printed text in this edition.
I’ve cut the advanced Internet topics chapter completely,
leaving only simple summaries at the start of the Internet part
(intentionally mirroring the GUI option summaries at the start of
the GUI part). This includes prior coverage for tools such as the
ZOPE web framework, COM, Windows active scripting and ASP,
HTMLgen, Python Server Pages (PSP), Jython, and the now very dated
Grail system. Some of these systems still receive honorable
mention in the summaries, but none are now presented in any sort
of detail. Summaries of new tools (including many of those listed
in the following paragraph) were added to this set, but again, in
brief fashion with no example code.
Despite authors’ best attempts to foresee the future, the
Web domain evolves faster than books like this can. For instance,
Web frameworks like Django,
Google’s
App Engine, TurboGears,
pylons, and web2py are now popular alternatives to ZOPE.
Similarly, the .NET framework supersedes much of COM on Windows;
Iron
Python
now provides the same type of
integration for .NET as Jython did first for Java; and active
scripting has been eclipsed by AJAX and JavaScript-oriented
frameworks on the client such as Flex, Silverlight, and pyjamas
(generally known today as rich Internet applications, RIAs).
Culture shift aside, the examples formerly presented in this
category were by themselves also insufficient to either teach or
do justice to the subject tools.
Rather than including incomplete (and nearly useless)
coverage of tools that are prone to both evolution and demise
during this edition’s expected lifespan, I now provide only brief
overviews of the current hot topics in the Web domain, and I
encourage readers to search the Web for more details. More to the
point, the goal of the book you’re reading is to impart the sort
of in-depth knowledge of Internet and Web fundamentals that will
allow you to use more advanced systems well, when you’re ready to
take the leap.
One exception here: the XML material of this prior chapter
was spared and relocated in expanded form to the text processing
chapter (where it probably belonged all along). In a related vein,
the coverage of ZOPE’s ZODB object-oriented database was retained,
although it was shortened radically to allow new coverage of ORMs
such as SQLObject and SQLAlchemy (again, in overview form).
At this writing, Python 3.X is still in its adoption phase,
and some of the third-party tools that this book formerly employed
in its examples are still available in Python 2.X form only. To
work around this temporary flux, I’ve changed some code to use
alternatives that already support 3.X today.
The most notable of these is the SQL database section—this
now uses the in-process SQLite library, which is a standard part
of Python and already in 3.X form, rather than the
enterprise-level MySQL interface which is still at 2.X today.
Luckily, the Python portable database API allows scripts to work
largely the same on both, so this is a minor pragmatic
sacrifice.
Of special note, the PIL extension used to display JPEGs in
the GUI part was ported to 3.1 just when it was needed for this
update, thanks to Fredrik Lundh. It’s still not officially
released in 3.X form as I submit the final draft of this book in
July 2010, but it should be soon, and 3.X patches are provided in
the book examples package as a temporary measure.
More advanced Python language tools such as descriptors,
properties, decorators, metaclasses, and Unicode text processing
basics are all part of the core Python language. Because of that,
they are covered in the Fourth Edition of
Learning
Python
, not here. For example, Unicode text and the
changes it implies for files, filenames, sockets, and much more
are discussed as encountered here, but the fundamentals of Unicode
itself are not presented in complete depth. Some of the topics in
this category are arguably application-level related too (or at
least of interest to tool builders and API developers in general),
but their coverage in
Learning
Python
allows us to avoid additional growth
here. Please see that book for more on these subjects.
Naturally, there were additional smaller changes made along
the way. For example, tkinter’sgrid
method is used instead ofpack
for layout of most input forms,
because it yields a more consistent layout on platforms where
label font sizes don’t match up with entry widget height
(including on a Windows 7 netbook laptop, this edition’s
development machine). There’s also new material scattered
throughout, including a new exploration of redirecting streams to
sockets in the Internet part; a new threaded and Unicode-aware
“grep” dialog and process-wide change tests on exit in the
PyEdit
example; and other things you are
probably better off uncovering along the way than reading further
about in this Preface.
I also finally replaced some remaining “#” comment blocks at
the top of source files with docstrings (even, for consistency, in
scripts not meant to be imported, though some “#” lines are
retained in larger examples to offset the text); changed a few
lingering “while 1” to “while True”; use+=
more often; and cleaned up a few
other cases of now-dated coding patterns. Old habits may die hard,
but such updates make the examples both more functional and more
representative of common practice today.