Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Accessing and updating global variables among several modules

Reply
Thread Tools

Accessing and updating global variables among several modules

 
 
Fuming Wang
Guest
Posts: n/a
 
      07-12-2003
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
 
Reply With Quote
 
 
 
 
Andrew MacIntyre
Guest
Posts: n/a
 
      07-12-2003
[posted & mailed]
On Sat, 11 Jul 2003, Fuming Wang wrote:

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

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: http://www.velocityreviews.com/forums/(E-Mail Removed) (pref) | Snail: PO Box 370
(E-Mail Removed) (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

 
Reply With Quote
 
 
 
 
Bryan
Guest
Posts: n/a
 
      07-12-2003
"Fuming Wang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      07-12-2003

"Fuming Wang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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




 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      07-12-2003
(E-Mail Removed) (Fuming Wang) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-12-2003
On 11 Jul 2003 20:38:48 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-17-2003
On 17 Jul 2003 05:28:49 -0700, (E-Mail 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
 
Reply With Quote
 
=?iso-8859-1?q?Fran=E7ois_Pinard?=
Guest
Posts: n/a
 
      07-17-2003
[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!

--
François Pinard http://www.iro.umontreal.ca/~pinard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can regular expressions be used to choose among several imperfectmatches? Ted Byers Perl Misc 1 11-18-2008 08:27 PM
RDOC: several related modules in several C files Victor \Zverok\ Shepelev Ruby 3 03-16-2007 04:15 PM
Sharing a method (function) among several objects. xifxif@gmail.com Python 1 02-27-2005 04:17 PM
How to stop a thread among several threads created with a singleRunnable ? Richard Java 11 05-04-2004 01:54 PM



Advertisments