When you import var
into mod1
:
from mod2 import var,fun_of_mod2
You give it the name var
in the mod1 namespace. It is as if you did this:
import mod2 var = mod2.var fun_of_mod2 = mod2.fun_of_mod2 del mod2
In other words, there are now two names for the value, mod1.var
and mod2.var
. They match at first, but when you reassign mod1.var
, mod2.var
still points to the same thing.
What you want to do is simply:
import mod2
Then select and assign the variable as mod2.var
.
It is important to note that global variables in Python are not truly global. They are global only for the module in which they are declared. To access global variables inside another module, you use the module.variable
syntax. The global
operator can be used inside a function that allows you to assign a global-global name (without it, assigning a variable makes it a local variable in this function). This has no other effect.
source share