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

(yzsuai) #1
In fact, the standard library is so powerful that it is not uncommon to hear Python
described as batteries included—a phrase generally credited to Frank Stajano meaning
that most of what you need for real day-to-day work is already there for importing.
Python’s standard library, while not part of the core language per se, is a standard part
of the Python system and you can expect it to be available wherever your scripts run.
Indeed, this is a noteworthy difference between Python and some other scripting lan-
guages—because Python comes with so many library tools “out of the box,” supple-
mental sites like Perl’s CPAN are not as important.
As we’ll see, the standard library forms much of the challenge in Python programming.
Once you’ve mastered the core language, you’ll find that you’ll spend most of your time
applying the built-in functions and modules that come with the system. On the other
hand, libraries are where most of the fun happens. In practice, programs become most
interesting when they start using services external to the language interpreter: networks,
files, GUIs, XML, databases, and so on. All of these are supported in the Python
standard library.
Beyond the standard library, there is an additional collection of third-party packages
for Python that must be fetched and installed separately. As of this writing, you can
find most of these third-party extensions via general web searches, and using the links
at http://www.python.org and at the PyPI website (accessible from http://www.python
.org). Some third-party extensions are large systems in their own right; NumPy, Django,
and VPython, for instance, add vector processing, website construction, and visuali-
zation, respectively.
If you have to do something special with Python, chances are good that either its support
is part of the standard Python install package or you can find a free and open source
module that will help. Most of the tools we’ll employ in this text are a standard part of
Python, but I’ll be careful to point out things that must be installed separately. Of
course, Python’s extreme code reuse idiom also makes your programs dependent on
the code you reuse; in practice, though, and as we’ll see repeatedly in this book, pow-
erful libraries coupled with open source access speed development without locking you
into an existing set of features or limitations.

System Scripting Overview


To begin our exploration of the systems domain, we will take a quick tour through the
standard library sys and os modules in this chapter, before moving on to larger system
programming concepts. As you can tell from the length of their attribute lists, both of
these are large modules—the following reflects Python 3.1 running on Windows 7
outside IDLE:


C:\...\PP4E\System> python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (...)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os
>>> len(dir(sys)) # 65 attributes
65

System Scripting Overview | 75
Free download pdf