Please have a look at that "module":
"""Module a"""
a = None
b = None
def gna():
global a
if a is None:
global b
a = 7
b = "b"
print("in a.py: a={}".format(a))
print("in a.py: b={}".format(b))
I would have thought that calling gna() from another module would initialise the variables:
"""Module b"""
from a import a, b, gna
print("in b.py: a={}".format(a))
print("in b.py: b={}".format(b))
gna()
print("in b.py: a={}".format(a))
print("in b.py: b={}".format(b))
But:
% python3 b.py
in b.py: a=None
in b.py: b=None
in a.py: a=7
in a.py: b=b
in b.py: a=None
in b.py: b=None
And I don't really get why a and b are None after calling gna...
Once you import a name into a module, the name becomes local. You should import module a instead of importing variables a and b from module a so that module b would be able to access the same references to variables a and b whose values the function gna modifies:
"""Module b"""
import a
print("in b.py: a={}".format(a.a))
print("in b.py: b={}".format(a.b))
a.gna()
print("in b.py: a={}".format(a.a))
print("in b.py: b={}".format(a.b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With