ankur ankur - 4 months ago 9
Python Question

Sharing Global variables in python

I have 2 files a.py and b.py as follows:

a.py

import b.py
Test="abc"
def main():
global Test
Test="klm"
b.fun()
if __name__ == "__main__":
main()


b.py

from a import Test
print Test


Output: abc

So basically my question is why is the Test variable not getting updated in b.py and how can I make this work? Thanks.

Answer
import a
a.main()
print a.Test
a.Test = "new Value"
print a.Text

You never invoke the main function. When you import a module, __name__ is not "__main__", so your main() never runs. When you run a.py directly it will run main()

Comments