February 2006
Intermediate to advanced
648 pages
14h 53m
English
You can turn any valid source file into a module by loading it with the import statement. For example, consider the following code:
# file : spam.py
a = 37 # A variable
def foo: # A function
print "I'm foo"
class bar: # A class
def grok(self):
print "I'm bar.grok"
b = bar() # Create an instanceTo load this code as a module, you use the statement import spam. The first time import is used to load a module, it does three things:
It creates a new namespace that serves as a namespace to all the objects defined in the corresponding source file. This is the namespace accessed when functions and methods defined within the module use the global statement.
It executes the code contained in the module within the newly created namespace.
It creates ...