【发布时间】:2021-05-07 18:15:52
【问题描述】:
我有两个模块,每个模块都包含一个类。在每个模块中,我想引用另一个模块中类的方法。我设置了一个小班来帮助我理解这个概念。我将一个模块导入另一个模块,但仍然收到循环导入错误。据我了解,这是正确的方法,但我仍然遇到错误。 这是我的示例类:
a.py 模块:
import b
class A():
def __init__(self):
print("A has run")
def aa():
print("aa has run")
b.B.bb()
b.py 模块:
import a
class B():
def __init__(self):
print("B has run")
def bb():
print("bb has run")
# Run method from class in seperate module
a.A.aa()
这是我的错误:
AttributeError: partially initialized module 'b' has no attribute 'B' (most likely due to a circular import)
【问题讨论】:
-
最简单的解决方案是将两个类放在同一个模块中。无论如何,
a.A.aa()和b.B.bb()没有意义。你的方法不是方法。所以很可能,只需将这些函数作为模块级函数移动到它们自己的模块中
标签: python