import globalvar as gl import show import logging def main(): logging.info("I'm in main.py") A = "main Hello Word" gl.set_value('main_string', A) logging.info("Set_value => " + A) show.show_str() if __name__ == "__main__": # Create log file FORMAT = '%(filename)s[line:%(lineno)d] %(levelname)s => %(message)s' logging.basicConfig(level=logging.DEBUG, filemode='w', format=FORMAT) # 宣告全域變數 gl._init() main()
def _init(): global _global_dict _global_dict = {} def set_value(name, value): _global_dict[name] = value def get_value(name, defValue=None): try: return _global_dict[name] except KeyError: return defValue
import globalvar as gl import logging def show_str(): logging.info("I'm in show.py") A = gl.get_value('main_string') logging.info("Get_value => " + A)
main.py[line:6] INFO => I'm in main.py main.py[line:9] INFO => Set_value => main Hello Word show.py[line:5] INFO => I'm in show.py show.py[line:7] INFO => Get_value => main Hello Word