One of the good features is the lack of gotchas. I define gotchas as traps for the unwary programmer, where something unexpected happens. Thankfully these are rare in Python. But the following is one that is worth knowing about. Consider two Python modules, named main_module.py and sub_module.py.
First main_module.py:
g_main_value = None def get_main_value(): return g_main_value def main_test(): print("sub_value = %s" % get_sub_value()) from sub_module import get_sub_value, sub_test, sub_init def main_init(): global g_main_value g_main_value = 23 if __name__ == "__main__": main_init() sub_init() main_test() sub_test() print("main_value (in main_module) = %s" % get_main_value() )
Then sub_module.py:
g_sub_value = None def get_sub_value(): return g_sub_value def sub_test(): print("main_value (in sub_module) = %s" % get_main_value()) def sub_init(): global g_sub_value g_sub_value = 31 from main_module import get_main_value
The output is:
sub_value = 31 main_value (in sub_module) = None main_value (in main_module) = 23
What’s happening? A global with 2 different values? Add the following code to the bottom of main_module:
def show_globals(module_name): mod = __import__(module_name) print(module_name) for k in dir(mod): if k.startswith("g_"): print(" %s: %s" % (k, getattr(mod, k))) print("") if __name__ == "__main__": print("") show_globals("__main__") show_globals("main_module") show_globals("sub_module")
The output is:
__main__ g_main_value: 23 main_module g_main_value: None sub_module g_sub_value: 31
This gives us a clue as to what’s happening: the __main__ module may be loaded twice if used by another module. If you wanted to set a global in the __main__ module that is usable by other modules, then one way to do it is:
Add the following to main_module.py
mod = __import__("main_module") setattr(mod, "g_main_value2", 34) if __name__ == "__main__": print("") show_globals("__main__") show_globals("main_module") show_globals("sub_module")
And the output will be
__main__ g_main_value: 23 g_main_value2: None main_module g_main_value: None g_main_value2: 34 sub_module g_sub_value: 31