【发布时间】:2014-10-10 04:41:35
【问题描述】:
这是我不工作的代码(简化):
文件 A.py:
from B import *
class A(object):
def __init__(self):
A.instance = self
self.b = B()
def testA(self):
print "success"
if __name__ == '__main__':
a = A()
a.b.testB()
文件 B.py:
from A import *
class B(object):
def testB(self):
A.instance.testA()
在 B.py 的最后一行失败:type object 'A' hast no attribute 'instance'。如果 A 和 B 在同一个文件中,它可以工作。如何将其拆分为多个文件以防止将整个项目放在一个巨大的文件中?
【问题讨论】:
-
直到你运行
A.__init__,A.instance是未定义的。你到底想达到什么目的?! -
@jonrsharpe:A.__init__ 正在运行,因为他执行
a = A()。但不是正确的A,因为模块被导入了两次。
标签: python python-2.7