【问题标题】:Why super() is calling constructor of class Parent1 only and not calling constructor of class Parent2?为什么 super() 只调用 Parent1 类的构造函数而不调用 Parent2 类的构造函数?
【发布时间】:2021-05-07 15:34:45
【问题描述】:
class Parent1:
    def __init__(self):
        self.data1 = 10
        print("parent1")

class Parent2:
    def __init__(self):
        self.data2 = 20 
        print("parent2")

class child(Parent2,Parent1):
    def __init__(self):
        print("this is child class")
        super().__init__()
        print(self.data1)
        print(self.data2)
        
obj = child()

输出:

this is child class
parent2
Traceback (most recent call last):
  File "f:/Project2020/Rough Work/rough2.py", line 18, in <module>
    obj = child()
  File "f:/Project2020/Rough Work/rough2.py", line 15, in __init__
    print(self.data1)
AttributeError: 'child' object has no attribute 'data1'

【问题讨论】:

  • Parent2.__init__ 不会调用 super().__init__,因此不会调用 Parent1.__init__
  • 当您设计多重继承时,所有类都需要在其方法中调用超级实现。
  • 为什么 super().__init__() 没有调用 Parent2 类的 __init__()?

标签: python inheritance parent-child super base-class


【解决方案1】:

回答你的问题:在 python 中有一个继承链。在您的情况下,这个继承链看起来像 [Child, Parent2, Parent1],
所以对于python编译器来说,没有多重继承之类的东西。 super() 只是在继承链上走一步的捷径。因此,您的声明等于Parent2.__init__(),实际上super().__init__() 调用Parent2.__init__() 应该如此。
所以你还需要在Child类中调用Parent1.__init__()

我建议写作:

class Parent1:
    def __init__(self):
        self.data1 = 10
        print("parent1")

class Parent2:
    def __init__(self):
        self.data2 = 20 
        print("parent2")

class Child(Parent2,Parent1):
    def __init__(self):
        print("this is child class")
        Parent2.__init__(self)
        Parent1.__init__(self)
        print(self.data1)
        print(self.data2)

在这种情况下,如果您不使用super(),代码会更清晰。这段代码可能看起来很奇怪,但在这里使用 super() 无论如何都会让人感到困惑。

【讨论】:

  • 你的方法很好。 super() 是否只调用第一个父类 __init__()?
  • 我对您的回答很满意。你能帮我解决这个问题吗-stackoverflow.com/q/66028530/12945919。它也是基于 super() 和多重继承的。
  • 谢谢。我知道我无法在那里回答你的问题,因为人们关闭了它。 (所以,我会在这里回答,如果对您有帮助,请在此处加 1)。因此,由于您的对象具有继承链 [Class4, Class2, Class3, Class1],因此 super() 遍历 that 链,而不是您自然期望的相应类的链。跨度>
  • 事实上,你甚至可以调用Class2.m(my_object),然后super() 将只是走my_object-s 链,可能会输出一些更奇怪的东西。
  • 我的问题在这里 - ide.geeksforgeeks.org/DNTVYZRn7B。请看一下。
猜你喜欢
  • 2013-01-28
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 2015-04-24
  • 2018-02-28
  • 2016-07-19
相关资源
最近更新 更多