Accessing and updating global variables among several modules

Michele Simionato mis6 at pitt.edu
Sat Jul 12 15:14:21 EDT 2003


fumingw at tom.com (Fuming Wang) 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'

Are you aware that in this way your creating a *local* variable gA
(local to the function init_fuct) without touching the original 
gA variable? Try

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

HTH,

         Michele




More information about the Python-list mailing list