[Tutor] global question
Alan Gauld
alan.gauld at btinternet.com
Sun Aug 12 01:35:25 CEST 2007
"Ara Kooser" <ghashsnaga at gmail.com> wrote
> I am back working on a text adventure game using functions. I
> broke
> down the game into areas (modules) but I am having trouble getting
> the
> other modules to recognize the globals I have set in the main
> program.
Yes you can't see from a module into the client that is importing.
The solution is either: don't use golobals (usually by creating
classes
that hold the "global" data internally. - this is the "best" solution
from
a programming/ computer science point of view. Or, put all the globals
into a module which can be imported by all other modules.
Like this:
# globals.py
foo = 42
bar = 27
# mymod.py
import globals
def f():
print globals.foo + globals.bar
# main.py
import globals, mymod
globals.foo = 125
mymod.f()
Its obviously more verbose and poses some restrictions in that
you are sharing data and any attempt at multi-threading will
surely end in tears, but otherwise it should work...
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list