ankur ankur - 1 year ago 100
Python Question

Sharing Global variables in python

I have 2 files and as follows:

def main():
global Test
if __name__ == "__main__":

from a import Test
print Test

Output: abc

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

Answer Source
import a
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 directly it will run main()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download