module parameters

J

Jim Jewett

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.

-jJ
 
C

Carl Banks

Jim said:
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.
 
N

Noah from IT Goes Click

Have 2 modules a wrapper and then the foundation. The user can load the
real module which basically just lets them create an initialization
object which then sets a bunch of very oddly named globals and then
loads your desired module which looks for those.. Sort of an icky way of
doing it but it's not bad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,578
Latest member
LC_06

Latest Threads

Top