global and import
Fredrik Lundh
fredrik at pythonware.com
Wed Jul 28 15:00:38 EDT 1999
Nathan Clegg <nathan at islanddata.com> wrote:
> I'm having a little trouble understanding how global and import interact.
quick answer: they don't.
"global" doesn't have anything to do with global
variables in the usual sense; it's just a way to
tell Python to look in the *module* namespace
instead of in the *local* namespace when you're
inside a function or a method.
> Currenly I have a script that includes a couple of global variables that
> allow for a small amount of communication between multiple threads and
> signal handlers. I would like to move some of the functions to separate
> modules for several reasons, but need to maintain just the two global
> variables between them.
put these two variables in a common module,
and access them explicitly from the others. and
just forget about the global statement.
consider this:
common.py:
mode = 0 # default
onemodule.py:
import common
def myfunc(self):
print "look ma, no global"
common.mode = 1
mylib.dosomething()
common.mode = 0
twomodule.py:
import common
def new_great_taste(argh):
compute(common.mode, argh)
etc.
</F>
More information about the Python-list
mailing list