[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
An analogy might help underscore the importance of scriptability. There are still a few
of us who remember a time when it was completely normal for car owners to work on
and repair their own automobiles. I still fondly remember huddling with friends under
the hood of a 1970 Camaro in my youth, tweaking and customizing its engine. With a
little work, we could make it as fast, flashy, and loud as we liked. Moreover, a break-
down in one of those older cars wasn’t necessarily the end of the world. There was at
least some chance that I could get the car going again on my own.
That’s not quite true today. With the introduction of electronic controls and diaboli-
cally cramped engine compartments, car owners are usually better off taking their cars
back to the dealer or another repair professional for all but the simplest kinds of
changes. By and large, cars are no longer user-maintainable products. And if I have a
breakdown in my shiny new ride, I’m probably going to be completely stuck until an
authorized repair person can get around to towing and fixing it.
I like to think of the closed and open software models in the same terms. When I use
Microsoft-provided programs such as Notepad and Outlook, I’m stuck with the feature
set that a large company dictates, as well as any bugs that it may harbor. But with
programmable tools such as PyEdit and PyMailGUI, I can still get under the hood. I
can add features, customize the system, and work my way out of any lurking bugs. And
I can do so long before the next Microsoft patch or release is available. I’m no longer
dependent on a self-interested company to support—or even to continue producing—
the tools I use.
Of course, I’m still dependent on Python and whatever changes it may dictate over time
(and after updating two 1,000+ page books for Python 3.X, I can say with some con-
fidence that this dependency isn’t always completely trivial). Having all the source code
for every layer of the tools you depend on, though, is still a powerful last resort, and a
major net win. As an added bonus, it fosters robustness by providing a built-in group
of people to test and hone the system.
At the end of the day, open source software and Python are as much about freedom as
they are about cost. Users, not an arbitrarily far-removed company, have the final say.
Not everyone wants to work on his own car, of course. On the other hand, software
tends to fail much more often than cars, and Python scripting is generally less greasy
than auto mechanics.

PyEdit: A Text Editor Program/Object


In the last few decades, I’ve typed text into a lot of programs. Most were closed systems
(I had to live with whatever decisions their designers made), and many ran on only one
platform. The PyEdit program presented in this section does better on both counts:
according to its own Tools/Info option, PyEdit implements a full-featured, graphical
text editor program in a total of 1,133 new lines of portable Python code, including
whitespace and comments, divided between 1,088 lines in the main file and 45 lines of
configuration module settings (at release, at least—final sizes may vary slightly in future
revisions). Despite its relatively modest size, by systems programming standards,


674 | Chapter 11: Complete GUI Programs

Free download pdf