I'm trying to get a better understanding of python imports and namespaces. Take the example of a module testImport.py:
# testImport.py
var1 = 1
def fn1():
return var1
if I import this like this:
from testImport import *
I can inspect var1
>>> var1
1
and fn1() returns
>>> fn1()
1
Now, I can change the value of var1:
>>> var1 = 2
>>> var1
2
but fn1() still returns 1:
>>> fn1()
1
This is different to if I import properly:
>>> import testImport as t
>>> t.var1 = 2
>>> t.fn1()
2
What's going on here? How does python 'remember' the original version of var1 in fn1 when using import *?
import *you are copying the references to every variable. Each module will have it's own version. This is a major reason why you don't want to useimport *