Accessing and updating global variables among several modules

F

Fuming Wang

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
change gA in module A after some initialization:
def init_fuct():
gA = 'new'

no change in module B:
print A.gA
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
 
A

Andrew MacIntyre

[posted & mailed]
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 *)

Use a container, and change the container's contents, eg make the global
"variable" a dictionary, and access the different "global variables" via
the dictionary's keys.

What you're seeing is due to Python's variables being references to
values, rather than actual values. With the container approach, all the
references will still point to the container, even though the contents
change. In fact, Python itself does pretty much all name lookup through
dictionaries.
 
B

Bryan

Fuming Wang said:
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

change gA in module A after some initialization:
def init_fuct():
gA = 'new'

no change in module B:
print A.gA

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


bryan
 
B

Bryan

Fuming Wang said:
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

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
 
M

Michele Simionato

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

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
 
B

Bengt Richter

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

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
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
 
B

Bengt Richter

On 17 Jul 2003 05:28:49 -0700, (e-mail address removed) (Fuming Wang) wrote:
[...]
Hi,

Thanks for the replies. I have actually found a solution to the
problem. The problem is caused by Python creating two copies of the
module that is passed to the interpreter. Here is a short report of
what the problem is and how to avoid it. Hope this can be of help for
others.

Fuming

P.S. I am running Python 2.3 b2 on Windows2000
[...
Very nice and clear exposition and demonstration of
an importing gotcha and how to avoid it.
....]

Thank you for writing that up so well. I am sure it will be of help.

Regards,
Bengt Richter
 
?

=?iso-8859-1?q?Fran=E7ois_Pinard?=

[Fuming Wang]
The problem is caused by Python creating two copies of the module that is
passed to the interpreter.

We were recently bitten by a variation on this problem.

A co-worker and I were writing one module each for a single project, both
modules were to derive classes from a common base class from the same module
`listes'. Everything was to be installed in a single package `Lc'.

In his module, he wrote:

from Lc import listes

while in my module I wrote:

import listes

In fact, both `import' worked, yet `listes' was not the same object for each
of our viewpoints, and so, our classes did not have a common base. Object
initialisation was modifying a supposedly common registry of created
objects, kept as a class variable in the base, so there was a problem.

P.S. - Or something similar, I'm not sure I remember correctly. So many
things happen between a particular day and the next one! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top