【发布时间】:2011-01-28 01:59:41
【问题描述】:
我有 2 个打印单例实例的文件,但我得到了两个不同的实例。
我正在使用来自Gary Robinson 的单例代码。
这里是文件:
test.py
>from singleton import Singleton
import untitled
class A(Singleton):
def __init__(self):
super(A, self).__init__()
if __name__ == "__main__":
a = A.getInstance()
print a
untitled.print_a()
untitled.py
>def print_a():
import test
print test.A.getInstance()
...这是python test.py的输出
<__main__.A object at 0xaea270>
<test.A object at 0xaea3f0>
有人可以向我解释导致这种行为的原因(显然是在模块级别)吗?
【问题讨论】:
-
test2模块中有什么? test2 是否从test.py导入A?
标签: python module singleton instances