How to pass parameter to a module?

Hung Jung Lu hungjunglu at yahoo.com
Fri Sep 19 03:28:49 EDT 2003


"M-a-S" <NO-MAIL at hotmail.com> wrote in message news:<Vwfab.2454$9G2.2174 at twister.southeast.rr.com>...
> I'd like to parametrize a module. That is, to set and pass
> some values into the module, while it is being imported.

There are many ways. 

(However, classes may fit your need better. "Passing parameters to a
module" is not a common practice, as far as I know.)

(1) Use a built-in namespace variable.

import __builtin__
__builtin__.myvar = 3
print myvar

This is a hack. I have been screamed at for mentioning it. :)

(2) Hack your favorite (non-builtin) module. The module could be any
of the standard library modules, or your own third module.

import sys
sys.myvar = 3

(3) Use environmental variables.

import os
os.environ['myvar'] = 'hello'

You get the idea. Python has three namespaces: built-in, global, and
local. Since global and local namespaces won't go over to the other
module, you need to rely on the built-in namespace, one way or
another. How you want to structure your data (by using the built-in
namespace, a module, a class, a dictionary or any other entities that
can hold a name entry), it's entirely up to you.

Hung Jung




More information about the Python-list mailing list