首页 > 技术文章 > Python实现跨文件全局变量的方法(2文件优化版)

zhujunsheng 2019-08-22 10:26 原文

 
全局变量管理模块 globalvar.py
 
def set_value(name, value):
global _global_dict
_global_dict = {}
_global_dict[name] = value

def get_value(name, defValue=None):
try:
return _global_dict[name]
except KeyError:
return defValue
set_value('score', 90)
 
 
2文件b.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import globalvar as gl

#name = gl.get_value('name')
score = gl.get_value('score')

print("%s" % (score))

推荐阅读