相关疑难解决方法(0)

python类之间的循环依赖

在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()
Run Code Online (Sandbox Code Playgroud)
  1. 为什么clases有这个问题,而函数没有?
  2. 像C++这样的语言有声明:class B要解决这种依赖,python是否有类似的结构?

python

7
推荐指数
1
解决办法
3657
查看次数

标签 统计

python ×1