module parameters

Carl Banks imbosol at aerojockey.invalid
Tue Apr 20 00:44:57 EDT 2004


Jim Jewett wrote:
> 
> 
> I have often wanted to pass arguments to a module.  
> 
> Tyically, these are globals, such as DEBUG or VERBOSE that are set 
> at the top of the module -- and I want to use a different value.  
> For modules that provide a service (such as logging), I would like 
> to make my changes before the defaults are set up.
> 
> Assuming that the loaded module is cooperative, what is the least 
> ugly way to pass these parameters?
> 
> (1)  Alter/Check __builtins__
> (2)  Alter/Check a "well-known" module, such as settings, for an 
> attribute named after the loading module.*
> (3)  Alter/check a modsettings module specific to module mod.
> (4)  Use an import hook
> (5)  Accept that there will be some inefficiencies and reset after 
> the initial load.
> 
> * I'm not aware of any such well-known module yet, but if others 
> agree that it sounds like a good idea, I will write one.


I'm not sure if it's a common enough need to have a standard way to do
it, if that's what you're suggesting.  I suspect most programmers, if
they do use such module "arguments", will design it so that they can
be set after the module is imported.  In fact, this is what I
recommend.  (Then again, there is PEP 329 on the table.)

But, if you really need these arguments set before importing the
module, then I recommend #2, and the well-known module to use is
__main__.  __main__ is the top level module, the one where the program
begins.  Just set whatever arguments you need at the top-level, and
have the cooperating module import __main__ and use whatever it needs.


-- 
CARL BANKS                      http://www.aerojockey.com/software
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon



More information about the Python-list mailing list