Accessing and updating global variables among several modules

Bryan belred1 at yahoo.com
Sat Jul 12 15:13:46 EDT 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'
>

the bug is here.... change your init_fuct() like this and everything should
work.

def init_fuct():
    global gA
    gA = 'new'


what happened is that you created a new local gA variable and never set your
global one.

bryan



> 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







More information about the Python-list mailing list