【发布时间】:2019-02-04 06:37:34
【问题描述】:
在 python 中,类定义可以像这样相互依赖:
# This is not fine
class A():
b = B().do_sth();
def do_sth(self):
pass
class B():
a = A().do_sth();
def do_sth(self):
pass
# This is fine
def FuncA():
b = FuncB()
def FuncB():
a = FuncA()
- 为什么类有这个问题,而函数没有?
- 像C++这样的语言有声明:
class B来解决这种依赖,python有类似的结构吗?
【问题讨论】:
-
A.do_sth()和B.do_sth()即使没有循环依赖也无法工作。您正在尝试在类本身上调用实例方法。 -
@user2357112 我很粗心。问题已编辑。
标签: python