Accessing and updating global variables among several modules

Discussion in 'Python' started by Fuming Wang, Jul 12, 2003.

  1. Fuming Wang

    Fuming Wang Guest

    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
    Fuming Wang, Jul 12, 2003
    #1
    1. Advertising

  2. [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: (pref) | Snail: PO Box 370
    (alt) | Belconnen ACT 2616
    Web: http://www.andymac.org/ | Australia
    Andrew MacIntyre, Jul 12, 2003
    #2
    1. Advertising

  3. Fuming Wang

    Bryan Guest

    "Fuming Wang" <> wrote in message
    news:...
    > 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
    Bryan, Jul 12, 2003
    #3
  4. Fuming Wang

    Bryan Guest

    "Fuming Wang" <> wrote in message
    news:...
    > 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
    Bryan, Jul 12, 2003
    #4
  5. (Fuming Wang) wrote in message news:<>...
    > 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
    Michele Simionato, Jul 12, 2003
    #5
  6. On 11 Jul 2003 20:38:48 -0700, (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
    Bengt Richter, Jul 12, 2003
    #6
  7. On 17 Jul 2003 05:28:49 -0700, (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
    Bengt Richter, Jul 17, 2003
    #7
  8. [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
    =?iso-8859-1?q?Fran=E7ois_Pinard?=, Jul 17, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    11
    Views:
    771
    Jesper Nordenberg
    May 4, 2004
  2. Replies:
    1
    Views:
    272
    Diez B. Roggisch
    Feb 27, 2005
  3. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    182
    Max Lapshin
    Mar 16, 2007
  4. Ted Byers
    Replies:
    1
    Views:
    75
  5. CM
    Replies:
    13
    Views:
    213
    Michael Torrie
    Feb 9, 2013
Loading...

Share This Page