[TriLUG] making a python module
cbc at unc.edu
Tue Feb 19 18:49:57 EST 2008
On 2/12/2008 10:27 AM, Joseph Mack NA3T wrote:
> What might you want to initialise in a python module on
> loading (and not running)?
I just saw this.
The most common case is a module will load more modules when loaded.
That is, you import a module and the first lines in the module you load
will be import statements which load yet more modules (and in turn,
those modules may import still more modules).
The module might selectively load certain other modules in a plug-in
architecture based on things the module can introspect from the
environment into which it was loaded.
You might want to cache some data from somewhere.
If you are doing metaprogramming in the module, you might want to create
some new objects of the metatype you just loaded.
If you've defined some factory methods in the module, you might want to
fire out some objects.
I think there's no end of uses for executing global namespace code when
loading a module. I can think of no case where a module would be useful
without executing code in its global namespace.
Basically, all the statements in a Python module are executed in order
when loaded, whether as a script or as an import. The execution of most
statements in a module, though, just define code objects (i.e., function
objects, class objects, etc.) which will be used later (in the script,
or by another script or module importing the module). So you might think
of that as function and class objects getting "initialized" with their
office: 332 Chapman Hall phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599
More information about the TriLUG