By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,534 Members | 1,322 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,534 IT Pros & Developers. It's quick & easy.

module parameters

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.