HomePython Page 9 - Python 101 (part 7): Dinner With A Hungry Giant
Bucking The System - Python
Python allows developers to logically group functions togetherinto modules, which can be imported and used by any Python program. In thisarticle, find out what a module is, learn how modules and module namespaceswork, and check out the default modules that ship with Python
A special mention should be made here of Python's "sys" module, which allows you to manipulate system-specific parameters like the list of currently-loaded modules and the module search path. The following example demonstrates the important attributes of this module:
As you can see, the first element of the "argv" list contains the name of the called script, with the remaining elements holding the command-line arguments.
Finally, the "__builtin__" module contains information on the various functions that are built into the interpreter. Take a look:
The errors you see are built-in exceptions - we'll be discussing them in detail in the next article.
More information on the various modules which ship with Python is available at http://www.python.org/doc/current/lib/lib.html
And that's about it for this discussion of Python modules. In this article, you found out how to logically group functions together into modules, which are the highest-level abstraction in Python. You examined two different techniques for importing module functions and variables into your own programs, together with the implications of each on your program's namespace. Finally, you found out a little bit more about the default modules that ship with Python, hopefully saving yourself some time the next time you sit down to code a Python program
In the next - and final - article of this series, I will be examining Python's error-handling routines, and demonstrating how to use them to trap and resolve program errors. Make sure that you come back for that one!
Note: All examples in this article have been tested on Linux/i586 with Python 1.5.2. Examples are illustrative only, and are not meant for a production environment. YMMV!