Let's consider a file called test1.py
and containing the following code:
def init_foo():
global foo
foo=10
Let's consider another file called test2.py
and containing the following:
import test1
test1.init_foo()
print(foo)
Provided that test1
is on the pythonpath (and gets imported correctly) I will now receive the following error message:
NameError: name 'foo' is not defined
Anyone can explain to me why the variable foo
is not declared as a global
in the scope of test2.py
while it is run? Also if you can provide a workaround for that problem?
Thx!