type-module
Modules
Modules are imported by the import statement. A module object has
a namespace implemented by a dictionary object (this is the dictionary
referenced by the func_globals attribute of functions defined in the
module). Attribute references are translated to lookups in this
dictionary, e.g., m.x is equivalent to m.__dict__["x"]. A
module object does not contain the code object used to initialize the
module (since it isn’t needed once the initialization is done).
Attribute assignment updates the module’s namespace dictionary, e.g., “m.x = 1” is equivalent to “m.__dict__[“x”] = 1”.
Special read-only attribute:
- __dict__ is the module’s namespace as a dictionary object.
Predefined (writable) attributes:
- __name__ is the module’s name.
- __doc__ is the module’s documentation string, or None if unavailable.
- __file__ is the pathname of the file from which the module was loaded, if it was loaded from a file.
The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.
Comment:
Merge with text from the library reference: > The only special operation on a module is attribute access: m.name, where m is a module and name accesses a name defined in m's symbol table. Module attributes can be assigned to. (Note that the import statement is not, strictly speaking, an operation on a module object; import foo does not require a module object named foo to exist, rather it requires an (external) definition for a module named foo somewhere.) > A special member of every module is \_\_dict\_\_. This is the dictionary containing the module's symbol table. Modifying this dictionary will actually change the module's symbol table, but direct assignment to the \_\_dict\_\_ attribute is not possible (you can write m.\_\_dict\_\_['a'] = 1, which defines m.a to be 1, but you can't write m.\_\_dict\_\_ = {}). Modifying \_\_dict\_\_ directly is not recommended. > Modules built into the interpreter are written like this: <module 'sys' (built-in)>. If loaded from a file, they are written as <module 'os' from '/usr/local/lib/python2.5/os.pyc'>.
Posted by infogami