Accessing and updating global variables among several modules

Bengt Richter bokr at oz.net
Sat Jul 12 16:21:02 EDT 2003


On 11 Jul 2003 20:38:48 -0700, fumingw at tom.com (Fuming Wang) wrote:

>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():
     global gA   # w/o this line, gA is just a local variable, and init_fuct() is well named ;-)
>    gA = 'new'
>
(I assume the above function was in module A, and either invoked from there or as A.init_fuct()
from somwhere else).

>no change in module B:
>print A.gA
>>>> 'old'
With the code above, there was no change in A either ;-)
>
>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 *)
>
Just make sure you actually change A.gA and you should be able to see the change from B as A.gA.
Unless I am missing something.

Regards,
Bengt Richter




More information about the Python-list mailing list