I l@ve RuBoard Previous Section Next Section

Chapter 8. Built-in Tools

This chapter presents a selection of the essential tools that make up the Python standard library梑uilt-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梖or 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梑uilt-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)]
I l@ve RuBoard Previous Section Next Section