See All Titles |
![]() ![]() Other Features of ModulesAuto-loaded ModulesWhen the Python interpreter starts up in standard mode, some modules are loaded by the interpreter for system use. The only one that affects you is the __builtin__ module, which normally gets loaded in as the __builtins__ module. The sys.modules variable consists of a dictionary of modules that the interpreter has currently loaded (in full and successfully) into the interpreter. The module names are the keys, and the location from which they were imported are the values. For example, in Windows, the sys.modules variable contains a large number of loaded modules, so we will shorten the list by requesting only the module names. This is accomplished by using the dictionary's keys() method: >>> import sys >>> sys.modules.keys() ['os.path', 'os', 'exceptions', '__main__', 'ntpath', 'strop', 'nt', 'sys', '__builtin__', 'site', 'signal', 'UserDict', 'string', 'stat'] The loaded modules for Unix are quite similar: >>> import sys >>> sys.modules.keys() ['os.path', 'os', 'readline', 'exceptions', '__main__', 'posix', 'sys', '__builtin__', 'site', 'signal', 'UserDict', 'posixpath', 'stat'] Preventing Attribute ImportIf you do not want module attributes imported when a module is imported with "from module import *", begin the name, and prepend the underscore ( _ ) to their names. Names in the imported module that begin with an underscore ( _ ) are not imported. This minimal level of data hiding does not apply if the entire module is imported.
|
© 2002, O'Reilly & Associates, Inc. |