The best way to get a feel for any book is to read it, of course. But especially for people who are familiar with the first edition, the next few sections go into more detail about what is new in this edition.
This edition has been updated for Python 2.0, and the Graphical User Interface (GUI) material has been updated for Tk versions 8.0 or later. Technically, this update was begun under Python 1.5.2, but all examples were revisited for 2.0 before publication.
For the trivia buffs among you: release 2.0 was the first Python release following Guido’s move to BeOpen, while 1.6 was the last release from Guido’s prior employer, CNRI. Just before I finished this book’s final draft and after the 2.0 release, Guido and the core Python development team moved from BeOpen to Digital Creations, home of the Zope web application construction kit, but this move is independent of Python releases (see Chapter 1, for more details).
Release 2.0 introduces a few language extensions, but 2.0 and 1.6 are similar in content, and the updates just add a handful of features. The examples in this book should generally work with later Python releases. Remarkably, almost all examples in the first edition still work five years later, with the latest Python releases; those that didn’t work required only small fixes (e.g., GUI call formats and C API interfaces).
On the other hand, although the core language hasn’t changed much since the first edition, a number of new constructs have been added, and we’ll apply them all here. Among these new Python features: module packages, class exceptions, pseudo-private class attributes, unicode strings, the new regular expression module, new Tkinter features such as the grid manager, standard dialogs, and top-level menus, and so on. A new appendix summarizes all of the major changes in Python between the first and second editions of this book.
In addition to the language changes, this book presents new Python tools and applications that have emerged in recent years. Among them: the IDLE programming interface, the JPython (a.k.a. “Jython”) compiler, Active Scripting and COM extensions, the Zope web framework, Python Server Pages (PSP), restricted execution mode, the HTMLgen and SWIG code generators, thread support, CGI and Internet protocol modules, and more (it’s been a busy five years). Such applications are the heart and soul of this second edition.
This edition presents Python programming by advanced examples. Becoming proficient in Python involves two distinct tasks: learning the core language itself, and then learning how to apply it in applications. This edition addresses the latter (and larger) of these tasks by presenting Python libraries, tools, and programming techniques. Since this is a very different focus, I should say a few words about its rationale here.
Because there were no other Python books on the horizon at the time, the first edition was written to appeal to many audiences at once -- beginners and gurus alike. Since then, another O’Reilly book, Learning Python, has been developed to address the needs of beginners, and Python Pocket Reference was published for readers seeking a short Python reference. As a result, the core language introductory-level material and the original reference appendixes have been removed from this book.
Learning Python introduces the core language -- its syntax, datatypes, and so on -- using intentionally simplistic examples. Many have found it to be ideal for learning the language itself, but Python can become even more interesting once you master the basic syntax and can write simple examples at the interactive prompt. Very soon after you’ve learned how to slice a list, you find yourself wanting to do real things, like writing scripts to compare file directories, responding to user requests on the Internet, displaying images in a window, reading email, and so on. Most of the day-to-day action is in applying the language, not the language itself.
Programming Python focuses on the “everything else” of Python development. It covers libraries and tools beyond the core language, which become paramount when you begin writing real applications. It also addresses larger software design issues such as reusability and OOP, which can only be illustrated in the context of realistically scaled programs. In other words, Programming Python, especially in this new edition, is designed to pick up where Learning Python leaves off.
Therefore, if you find this book too advanced, I encourage you to read Learning Python as a prelude to this text, and return here for the rest of the story once you’ve mastered the basics. Unless you already have substantial programming experience, this edition might serve you best as a second Python text.
Most of the changes in this edition were made to accommodate new topics. There are new chapters and sections on Internet scripting, CGI scripts, operating system interfaces, the SWIG integration code generator, advanced Tkinter topics, the HTMLgen web page generator, JPython, threads, restricted execution mode, and more. You should consult the Table of Contents for the full scoop, but here are some of the new topics and structural changes you’ll find in this edition:
The Internet, systems programming, Tkinter GUIs, and C integration domains get much more attention, and are arguably now the main focus of this text. For instance, you’ll find six new chapters on Internet scripting, covering client-side tools, server-side scripts and web sites, and advanced Internet topics and systems. Four new chapters address systems topics: threads, directory processing, program launching, and so on. And the GUI material has also grown from one chapter to a much more complete four-chapter presentation, and now covers all widgets (including text and canvas), as well as new grid, menu, and dialog support.
The C extending and embedding chapters have been expanded to cover new topics such as SWIG (the way to mix Python with C/C++ libraries today) and present new mixed-mode examples such as callback dispatch (extending plus embedding). C integration is at the heart of many Python systems, but the examples in this domain are inevitably complex, and involve large C programs that are only useful to C users. In deference to readers who don’t need to code C integrations, this material is now isolated at the end of the text. Some of the C code listings are gone as well -- to reduce page count, I have opted instead to point readers to C source files on the enclosed CD-ROM where possible (see http://examples.oreilly.com/python2).
Although later chapters build upon material in earlier chapters, topics in this edition are covered fairly independently, and are associated by book parts. Because of that, it’s not too much of a stretch to consider this edition to be akin to four or five books in one. Its top-level structure underscores its application topics focus:
|the preface (you are here)|
Two notes here: First of all, don’t let these titles fool you -- although most have to do with application topics, Python language features and general design concepts are still explored along the way, in the context of real-world goals. Second, readers who use Python as a standalone tool can safely skip the integration chapters, though I still recommend a quick glance. C programming isn’t nearly as fun or easy as Python programming. Yet because integration is central to Python’s role as a scripting tool, a cursory understanding can be useful, regardless of whether you do integrating, scripting, or both.
First edition readers will notice that most of this material is new, and even chapters with old titles contain largely new material. Noticeably absent in this edition are the original Sneak Preview, Mini Reference, Tutorial Appendix, and all of the old Part II -- a reflection of the new focus and intended readership.
This book is largely about its examples. In this edition, old examples have been extended to become more realistic (e.g., PyForm and PyCalc), and new examples have been added throughout. Among the major examples, you’ll find:
A Python/Tk text file editor object and program
A photo image and note-file slideshow
A paint program for drawing and moving image objects
A tree data structure drawing program
A Python/Tk analog and digital clock widget
An AI-powered graphical tic-tac-toe program
A persistent object table browser
A calculator widget in Python/Tk
A Python/Tk POP and SMTP email client
A simple Python/Tk file-transfer GUI
A web-based error report system
A web-based email interface
There are also new mixed-mode C integration examples (e.g., callback
registration and class object processing), SWIG examples (with and
without “shadow” classes for C++), more Internet examples
(FTP upload and download scripts, NNTP and HTTP examples, email
tools, and new
select module examples), many new examples of
Python threads, and new coverage of JPython, HTMLgen, Zope, Active
Scripting, COM, and Python database interfaces. Many of the new
examples are somewhat advanced, but of course this is now a somewhat
In addition, the old Python/C embedding API (now called ppembed ) has been extended to support precompiling strings to bytecode, and the original calculator example (now called PyCalc) has been beefed up to support keyboard entry, history lists, colors, and more.
In fact, the new book examples tree distributed on this edition’s CD-ROM (see http://examples.oreilly.com/python2) is itself a fairly sophisticated Python software system, and the examples within it have been upgraded structurally in a number of important ways:
The entire examples distribution has been organized as one big Python
facilitate cross-directory imports and avoid name-clashes with other
Python code installed on your computer. Using directory paths in
import statements (instead of a complex PYTHONPATH) also tends to
make it easier to tell where modules come from. Moreover, you now
need to add only one directory to your PYTHONPATH search-path setting
for the entire book examples tree: the directory containing the
PP2E examples root directory. To reuse code in
this book within your own applications, simply import through the
PP2E package root (e.g.,
Module names are generally much less cryptic now. I punted on 8.3 DOS compatibility long ago, and use more descriptive filenames. I’ve also fixed some old all-uppercase filenames, which were a last vestige of MS-DOS.
Labels of example listings now give the full directory pathname of
the example’s source file to help you locate it in the examples
distribution. For instance, an example source-code file whose name is
Example N-M: PP2E\Internet\Ftp\sousa.py
refers to the file
sousa.py in the
PP2E\Internet\Ftp subdirectory of the examples
Similarly, when a command line is shown typed after a prompt such as
C:\...\PP2E\System\Streams>, for example, it is
really to be typed in the
subdirectory in your examples tree. Unix and Linux users: please
think / when you see \ in filename paths (my official excuse for
which is outlined in the next section).
Because it’s just plain fun to click on things right away, there are new self-configuring demo launcher programs (described later in this preface in Section P.5.1), to give you a quick look at Python scripts in action with minimal configuration requirements. You can generally run them straight off the book’s CD without setting any shell variables first.
Except for some C integration examples, the majority of the programs in this edition were developed on my Windows 98 laptop, with an eye toward portability to Linux and other platforms. In fact, some of the examples were born of my desire to provide portable Python equivalents of tools missing on Windows (e.g., file splitters). When programs are shown in action, it’s usually on Windows; they are demonstrated on the Red Hat Linux 6.x platform only if they exercise Unix-specific interfaces.
This is not a political statement at all -- I like Linux too. It’s mostly a function of the fact that I wrote this book with MS Word; when time is tight, it’s more convenient to run scripts on the same platform as your publishing tools than to frequently reboot into Linux. Luckily, because Python has now become so portable to both Windows and Linux, the underlying operating system is less of a concern to Python developers than it once was. Python, its libraries, and its Tkinter GUI framework all work extremely well on both platforms today.
Because I’m not a politician, though, I’ve tried to make the examples as platform-neutral as possible, and point out platform-specific issues along the way. Generally speaking, most of the scripts should work on common Python platforms unchanged. For instance, all the GUI examples were tested on both Windows (98, 95) and Linux (KDE, Gnome), and most of the command-line and thread examples were developed on Windows but work on Linux too. Because Python’s system interfaces are generally built to be portable, this is easier than it may sound.
On the other hand, this book does delve into platform-specific topics where appropriate. There is new coverage of many Windows-specific topics -- Active Scripting, COM, program launch options, and so on. Linux and Unix readers will also find material geared towards their platforms -- forks, pipes, and the like. There is also new discussion of ways to edit and run Python programs on most major platforms.
The one place where readers may still catch a glimpse of platform biases is in the Python/C integration examples. For simplicity, the C compilation details covered in this text are still somewhat Unix/Linux-biased. One can at least make a reasonable case for such a focus -- not only does Linux come with C compilers for free, but its development environment grew up around that language. On Windows, the C extension code shown in this book will work, but you may need to use different build procedures that vary per Windows compiler. O’Reilly has published an outstanding text, Python Programming on Win32, that covers Windows-specific Python topics like this, and should help address some of the disparity here. If you do Windows-specific programming, please see that book for all Windows details skipped here.
 The “examples
distribution directory” is the directory containing the
PP2E directory of the book examples
tree. On the CD (see http://examples.oreilly.com/python2), it’s the topmost
directory; if you’ve copied the examples to your machine,
it’s wherever you copied (or unpacked) the
PP2E root directory. You can run most of the
examples from the CD directly, but you’ll want to copy them to
your hard drive to make changes, and to allow Python to save
.pyc compiled bytecode files for quicker