This chapter presents a selection of the essential tools that make up the Python standard library—built-in functions, library modules, and their most useful functions and classes. These are the sine qua non; while you most likely won’t use all of these in any one program, no useful program we’ve ever seen avoids all of these. Just as Python provides a list data structure object type because sequence manipulations occur in all programming contexts, the library provides a set of modules that will come in handy over and over again. Before designing and writing any piece of generally useful code, check to see if a similar module already exists. If it’s part of the standard Python library, you can be assured that it’s been heavily tested; even better, others are committed to fixing any remaining bugs—for free.
Note that this chapter gives only a brief look at the best of the standard library. As of current writing, the Python Library Reference is over 200 pages long. More details on the reference are available in Appendix A, but you should know that it’s the ideal companion to this book; it provides the completeness we don’t have the room for, and, being available online, is the most up-to-date description of the standard Python toolset. Also, O’Reilly’s Python Pocket Reference, written by coauthor Mark Lutz, covers the most important modules in the standard library, along with the syntax and built-in functions.
This chapter includes descriptions of two kinds of
tools—built-in functions and standard modules. Before we get to
those sections, however, we’ll say a brief word about built-in
objects. When introducing lists, for example, we’ve presented
their behavior as well as their most important methods
(append
, insert
,
sort
, reverse
,
index
, etc.). We have not been exhaustive in this
coverage in order to focus on the most important aspects of the
objects. If you’re curious about what we’ve left out, you
can look it up in the Library Reference, or you
can poke around in the Python interactive interpreter. Starting with
Python 1.5, the dir
built-in function returns a list of all
of the important attributes of objects, and, along with the
type
built-in, provides a great way to learn about
the objects you’re manipulating. For example:
>>>dir([])
# what are the attributes of lists? ['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort'] >>>dir(())
# what are the attributes of tuples? [] # tuples have no attributes! >>>dir(sys.stdin)
# what are the attributes of files? ['close', 'closed', 'fileno', 'flush', 'isatty', 'mode', 'name', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines'] >>> dir(sys) # modules are objects too ['__doc__', '__name__', 'argv', 'builtin_module_names', 'copyright', 'dllhandle' 'exc_info', 'exc_type', 'exec_prefix', 'executable', 'exit', 'getrefcount','maxint', 'modules', 'path', 'platform', 'prefix', 'ps1', 'ps2','setcheckinterval', 'setprofile', 'settrace', 'stderr', 'stdin', 'stdout','version', 'winver'] >>>type(sys.version)
# what kind of thing is 'version'? <type 'string'> >>>print sys.version
# what is the value of this string? 1.5 (#0, Dec 30 1997, 23:24:20) [MSC 32 bit (Intel)]
The sys
module contains several functions and
attributes internal to Python; sys
in this case
means Python system, not operating system. Some of the most useful
attributes are:
sys.path
A list containing the directories Python looks into when doing imports.
sys.modules
A dictionary of the modules that have been loaded in the current session.
sys.platform
A string referring to the current platform. Its possible values
include 'win32'
, 'mac'
,
'osf1'
, 'linux-i386'
,
'sunos4'
, etc. It’s sometimes useful to
check the value of sys.platform
when doing
platform-specific things (such as starting a window manager).
sys.ps1
and
sys.ps2
Two printable objects, used by Python in the interactive interpreter
as the primary and secondary prompts. Their default values are
...
and >>> . You can set them to strings
or to instances of classes that define a __repr
_
_ method.
One of the most frequently used attributes of the
sys
module
is
sys.argv
, a list of the words input on the command
line, excluding the reference to Python itself if it exists. In other
words, if you type at the shell:
csh> python run.py a x=3 foo
then when run.py
starts, the value of the
sys.argv
attribute is ['run.py', 'a', 'x=3', 'foo']
. The sys.argv
attribute is
mutable (after all, it’s just a list). Common usage involves
iterating over the arguments of the Python
program, that is, sys.argv[1:]
; slicing from index
1 till the end gives all of the arguments to the program itself, but
doesn’t include the name of the program (module) stored in
sys.argv[0]
.
Finally, there are three file attributes in the
sys
module: sys.stdin
,
sys.stdout
, and sys.stderr
.
They are references to the standard input, output, and error streams
respectively. Standard input is generally associated by the operating
system with the user’s keyboard; standard output and standard
error are usually associated with the console. The
print
statement in Python outputs to standard
output (sys.stdout
), while error messages such as
exceptions are output on the standard error stream
(sys.stderr
). Finally, as we’ll see in an
example, these are mutable attributes: you can redirect output of a
Python program to a file simply by assigning to
sys.stdout
:
sys.stdout = open('log.out', 'w')