04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ BATTERIES INCLUDED 215<br />

The Standard Library: A Few Favorites<br />

Chances are that you’re beginning to wonder what the title of this chapter means. The phrase<br />

was originally coined by Frank Stajano and refers to Python’s copious standard library. When<br />

you install Python, you get lots of useful modules (the batteries) for “free.” Because there are so<br />

many ways of getting more information about these modules (as explained in the first part of<br />

this chapter), I won’t include a full reference here (which would take up far too much space<br />

anyway), but I’ll describe a few of my favorite standard modules to whet your appetite for<br />

exploration. You’ll encounter more standard modules in the project chapters (Chapter 20 and<br />

later). The module descriptions are not complete but highlight some of the interesting features<br />

of each module.<br />

sys<br />

This module gives you access to variables and functions that are closely linked to the Python<br />

interpreter. Some of these are shown in Table 10-2.<br />

Table 10-2. Some Important Functions and Variables in the sys Module<br />

Function/Variable<br />

argv<br />

exit([arg])<br />

modules<br />

path<br />

platform<br />

stdin<br />

stdout<br />

stderr<br />

Description<br />

The command-line arguments, including the script name<br />

Exits the current program, optionally with a given return value or<br />

error message<br />

A dictionary mapping module names to loaded modules<br />

A list of directory names where modules can be found<br />

Contains a platform identifier such as sunos5 or win32<br />

Standard input stream—a file-like object<br />

Standard output stream—a file-like object<br />

Standard error stream—a file-like object<br />

The variable sys.argv contains the arguments passed to the Python interpreter, including<br />

the script name.<br />

The function sys.exit exits the current program. (If called within a try/finally block, the<br />

finally clause is executed.) You can supply an integer to indicate whether the program succeeded<br />

or not—a UNIX convention. You’ll probably be fine in most cases if you rely on the default<br />

(which is zero, indicating success). Alternatively, you can supply a string, which is used as an<br />

error message and can be very useful for a user trying to figure out why the program halted;<br />

then, the program exits with that error message and a code indicating failure.<br />

The mapping sys.modules maps module names to actual modules. It only applies to currently<br />

imported modules.<br />

The module variable sys.path was discussed earlier in this chapter. It’s a list of strings, in<br />

which each string is the name of a directory where the interpreter will look for modules when<br />

an import statement is executed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!