相关疑难解决方法(0)

如何避免使用super()进行无限递归?

我有这样的代码:

class A(object):
    def __init__(self):
          self.a = 1

class B(A):
    def __init__(self):
        self.b = 2
        super(self.__class__, self).__init__()

class C(B):
    def __init__(self):
        self.c = 3
        super(self.__class__, self).__init__()
Run Code Online (Sandbox Code Playgroud)

实例化B按预期工作但实例化C无限递归并导致堆栈溢出.我怎么解决这个问题?

python oop multiple-inheritance super

28
推荐指数
1
解决办法
4815
查看次数

标签 统计

multiple-inheritance ×1

oop ×1

python ×1

super ×1