普通的全局变量仅限于一个模块中。那所有模块都可以调用的全局变量该如何使用?
global_variable.py
#!/usr/bin/python # -*- coding: utf-8 -*- 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 def show_valud(): print("{}".format(_global_dict))
#!/usr/bin/python # -*- coding: utf-8 -*- import global_variable.py as gl gl.init() gl.set_value("a", 1) gl.set_value("b", "c") gl.set_value("c", {"m":12, "n":11}) gl.get_value("c")