Accessing and updating global variables among several modules

Bryan belred1 at yahoo.com
Sat Jul 12 21:09:44 CEST 2003


"Fuming Wang" <fumingw at tom.com> wrote in message
news:a12f6af8.0307111938.4d7a9851 at posting.google.com...
> Hi,
>
> I have several modules that need to access global variables among
> them.  I can do that by import modules:
>
> module A:
> gA = 'old'
>
> module B:
> import A
> print A.gA
> >>> 'old'
>
> change gA in module A after some initialization:
> def init_fuct():
>     gA = 'new'
>
> no change in module B:
> print A.gA
> >>> 'old'
>
> However, when I modify these global variables in one module, the other
> modules would not see the changes.  Any one has any suggestions? ( I
> don't want to use from A import *)
>
>
> Thanks,
> Fuming


i've used this technique before where different modules access a global
variable in another module it works.  i'm not understanding why you are
having problems.  i just did this test with strings... is this similar to
what you are doing?  it shouldn't matter how myvar gets set.

--- test1.py
myvar = 'abc'
def set_myvar(x):
    global myvar
    myvar = x

--- test2.py
import test1
print test1.myvar
test1.set_myvar('def')
print test1.myvar

--- from the interpreter
>>> import test2
abc
def
>>>


bryan






More information about the Python-list mailing list