相关疑难解决方法(0)

使用__init __()方法理解Python super()

我正在努力了解它的用法super().从它的外观来看,可以创建两个子类,就好了.

我很想知道以下2个孩子班级之间的实际差异.

class Base(object):
    def __init__(self):
        print "Base created"

class ChildA(Base):
    def __init__(self):
        Base.__init__(self)

class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()

ChildA() 
ChildB()
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class super

2366
推荐指数
7
解决办法
158万
查看次数

如何避免使用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
查看次数

标签 统计

oop ×2

python ×2

super ×2

class ×1

inheritance ×1

multiple-inheritance ×1