Python global variables and callbacks
python
Solution
Python imports the main module as `__main__`. When `b.py` imports `a` by its actual name, a new instance of the module is loaded under the name `a`. Each instance has its own `myGlobal`.
One solution is this:
#b.py
def runb():
from __main__ import callback
callback()
Another solution is to create a new main module. Import `a` there and make an explicit call to `a.main()`.
Problem
I'm writing a program that involves callbacks called from another module, and which need to access a global variable. It seems that changes assigned to the global variable are not seen in the callback function, which only sees the original assignment. I'm guessing due to the import from the other module. What is the proper way to write this pattern? First module: ``` # a.py from b import runb myGlobal=None def init(): global myGlobal myGlobal=1 def callback(): print myGlobal def main(): init() runb() if __name__=='__main__': main() ``` Second module: ``` #b.py def runb(): from a import callback callback() ``` I would expect this program to print '1', but instead it prints 'None' EDIT: init can only be called once (it is a simplification of a complex program)