1、创建一个字典用于保存变量
# dict
import collections
class GlobalDict(collections.UserDict):
"""使用继承UserDict"""
def __missing__(self, key):
"""获取不存在的值时"""
return None
my_dict = GlobalDict()
2、引用my_dict 保存全局变量
# global_dict
from dict import my_dict
my_dict['key1'] = 1
my_dict['key2'] = 2
3、使用my_dict作为全局变量的字典
from global_dict import my_dict
print(my_dict)