【发布时间】:2012-08-25 03:15:03
【问题描述】:
如何从其他 .py 文件继承全局变量?
示例:
globals.py
test = 'testValue'
index.py
from globals import *
def setGlobals():
global test
test = 'new Value' # Setting new value to project global
setGlobals()
help.py
from globals import *
print test # Should print out 'new Value', but it prints 'testValue'
我希望有一个文件充满常量和全局变量,这些变量由另一个文件设置并被其他文件使用。在这个例子中,我在index.py 中为全局变量test 设置了新值,并希望在help.py 中使用新值。我对 python 使用了错误的方法吗?谢谢。
编辑: 事件顺序与示例相同,例如首先我导入,设置新值,然后尝试从另一个文件打印出新设置的值。
【问题讨论】:
-
你没有在示例程序中调用
setGlobals。
标签: python inheritance globals