I'm having trouble understanding why importing a global variable from another module works as expected when using import
, but when using from x import *
the global variable doesn't appear to update within its own module
Imagine I have 2 files, one.py:
def change(value):
global x
x = value
x = "start"
and two.py:
from one import *
print x
change("updated")
print x
I'd expect:
start
updated
But I get...
start
start
If I import the module normally it works as expected
import one
print one.x
one.change("updated")
print one.x
Result...
start
updated
Given that I can't change ony.py's use of global variables (not my fault), and two.py is meant to be a sort of wrapper* around one.py, I'd really like to avoid using the one.
namespace throughout two.py for the sake of one stubborn variable.
If it's not possible a novice-level explantion of what's going on might help me avoid getting stuck like this again. I undertand that one.x is getting updated, but two.x isn't respecting the updates, but I don't know why.
import two; print x
doesn't make sense. I assume you meantimport two; print two.x; two.change...
. If you currently havefrom one import x
in any other files those files are already broken for the same reasons. – Reticulum