0

I have these 4 modules

globals.py

globvara = "a"

mod1.py

from globals import *

print globvara

output:

a

mod2.py

from mod1 import *

def changegv(newval1):
    #global globvara
    globvara = newval1

def usechangegv(newval2):
    changegv(newval2)

and mod3.py

from mod2 import *

usechangegv("b")

print globvara

output:

a
a

I am wondering why the global var does not change in module 2. I am missing something in global variables. Even if I uncomment the global globvara line I get the same result. Where is the error?

4
  • Out of interest, are you doing this as an experiment to get familiar with how scoping works in Python or do you actually have such case in a program you're writing? Commented Sep 4, 2014 at 22:59
  • I am working on a bigger project and I simplified it. Commented Sep 4, 2014 at 23:04
  • Related: stackoverflow.com/questions/25630386/… (but not a diff, I think, both because this question wants to be able to assign to globvara in each module, not just access it, and because he's not asking how to "work around" this problem, just asking what's happening). Commented Sep 4, 2014 at 23:21
  • As far as the assignment goes: Presumably you know that globvara = newval1 will create a new local even if there's a global of the same name, unless you uncomment that global statement. But even with that statement, it will create a new (module-)global even if there's a builtin of the same name, and there is no builtin statement to override that. Commented Sep 4, 2014 at 23:23

2 Answers 2

5

Python global variables are global only to modules. When you import a variable from another module (e.g. from mod1 import *), Python creates duplicate references to the value in the importing module. So you now have two names, mod1.globvara and mod2.globvara, which initially point to the same value, but which are not in any way connected. If you change globvara in mod2.py, you are changing mod2.globvara and mod1.globvara is not affected.

To avoid this problem, import the module, not the individual names defined in it. For example, import globals. Then always refer to globals.globvara (or better yet, globals.a). Since you are always accessing and assigning the same name, it will work the way you expect.

Sign up to request clarification or add additional context in comments.

3 Comments

As I have explained, you can't; not the way you want.
@Yannis: Actually, you can, by storing it in builtins instead of in a module. But you really, really don't want to. (See this answer for why not—and also, coincidentally, for how.)
Also be aware of the built-in function globals - better to use another name for your module.
1

Don't use the

from <module> import <variable>

As it creates a copy of the variable.

Do a simple:

import <module>

And all accesses to the global variable should use the "variable" within "module":

<module>.<variable> = ...

or

print <module>.<variable>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.