Python 静态变量
我正在创建一个应用程序,它使用基类来保存所有配置值、导入方法等。
/
- application.py
+ class foo
+ config = None
+ def loadconfig
- otherfile.py
+ class bar
+ def getconfigvalue
因此,如果我启动 application.py
并且它运行 loadconfig
,它将一个值加载到 foo.config
中,然后导入(在所述函数内 - 以绕过循环导入)otherfile.py
并创建一个新的 bar< /code> 对象,然后尝试从中获取配置值
foo.config
,但表示 foo.config
等于 None。有什么建议吗?
简化代码:
main.py
class Main:
config = None
@staticmethod
def start():
## Load the configuration from a file, creating a dict in Main.config ##
Main.other()
@staticmethod
def other():
from otherfile import otherclass
otherclass()
Main.start()
otherfile.py
from main import Main
class otherclass:
def __init__(self):
print(Main.config) ## Prints "None"
注意:这样安排是因为它在程序中实际是这样工作的;我觉得它与范围有关
完整源文件:
asgard.py: http://pastebin.com/jRkWzrPq
库/childcontainer.py: http://pastebin.com/6a561Nun
I'm creating an application that uses a base class to hold all of the configuration values, import methods, etc.
/
- application.py
+ class foo
+ config = None
+ def loadconfig
- otherfile.py
+ class bar
+ def getconfigvalue
So, if I start application.py
and it runs loadconfig
, which loads a value into foo.config
, and then imports (inside said function - to get around circular imports) otherfile.py
and creates a new bar
object, which then tries to get a configuration value from foo.config
, but says that foo.config
is equal to None. Any suggestions?
Simplified code:
main.py
class Main:
config = None
@staticmethod
def start():
## Load the configuration from a file, creating a dict in Main.config ##
Main.other()
@staticmethod
def other():
from otherfile import otherclass
otherclass()
Main.start()
otherfile.py
from main import Main
class otherclass:
def __init__(self):
print(Main.config) ## Prints "None"
Note: It was arranged like this because that's how it actually works in the program; I feel like it has something to do with scope
Full source files:
asgard.py: http://pastebin.com/jRkWzrPq
library/childcontainer.py: http://pastebin.com/6a561Nun
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将从我认为您的问题与 asgard 相关的内容开始工作,因为您的简化示例已损坏:
main.py
,但我相信它是main.py
您打算运行(运行otherfile.py
不会出现我认为您遇到的问题)。Main.config
。我不确定你打算分配给它的确切位置。无论如何,请转到
asgard.py
。这里你遇到了模块
__main__
的问题。当你运行asgard.py
时,它的__name__
是__main__
;您可能不知道的是,这实际上是它出现在 sys.modules 中的模块名称 - 主模块是 sys.modules['__main__'],不是 sys.modules['asgard']。然后,当您导入library.childcontainer
时,它会尝试导入asgard
。这会查找不存在的 sys.modules['asgard'],因此它将asgard.py
的内容导入到新的模块对象中。如果您有另一个文件
main.py
,它会import asgard; asgard.Asgard.initialize()
(忽略我下面提到的条件导入问题),您不会遇到此问题,因为__main__
模块将属于该main。 py
和asgard.py
只能以名称asgard
导入。另一个可行的解决方案是 if __name__ == '__main__': sys.modules['asgard'] = sys.modules['__main__']。请,请,请不要永远拉出
if __name__ == '__main__': import ...技巧。这意味着如果您尝试导入 asgard;例如,asgard.Asgard.initialize()
,它将失败,提示名称“os”未定义。请将这些导入放在文件的顶部,也就是它们所属的位置。I'll work from what I believe your issue is with asgard, because your simplified example is broken:
main.py
because of a circular import, yet I believe it wasmain.py
you intended to be running (runningotherfile.py
won't exhibit the problem I believe you're running into).Main.config
. I'm not sure precisely where you were intending to assign to it.Anyway, on to
asgard.py
.Here you run into the problem of the module
__main__
. When you runasgard.py
, its__name__
is__main__
; something you may not be aware of is that this is literally its module name as it appears insys.modules
- the main module issys.modules['__main__']
, notsys.modules['asgard']
. Then, when you importlibrary.childcontainer
, it tries to importasgard
. This looks upsys.modules['asgard']
, which doesn't exist, and so it imports the contents ofasgard.py
into a new module object.If you were to have another file
main.py
which didimport asgard; asgard.Asgard.initialize()
(ignoring the conditional imports problem I mention below), you wouldn't run into this problem because the__main__
module would be of thatmain.py
, andasgard.py
would only ever be imported with the nameasgard
. Another solution which would work would beif __name__ == '__main__': sys.modules['asgard'] = sys.modules['__main__']
.And please, please, please don't ever pull that
if __name__ == '__main__': import ...
trick. This means that if you try toimport asgard; asgard.Asgard.initialize()
, for example, it will fail saying that the name 'os' is undefined. Please put these imports at the top of the file, where they belong.