python中多重继承中的super()用法 [英] super() usage in multiple inheritance in python

查看:41
本文介绍了python中多重继承中的super()用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手.我试图了解 python 多重继承中的 super() 功能.

class B():def __init__(self):打印(调用 B 的 __init__")self.b = "B"C类():def __init__(self):打印(调用 C 的 __init__")self.c = "C"D类(B、C):def __init__(self):打印(调用 D 的 __init__")super().__init__()定义输出(自我):打印(self.b,self.c)d = D()d.输出()

我收到以下错误:

AttributeError: 'D' 对象没有属性 'c'

解决方案

super() 将在 MRO 序列中查找 next 方法.这意味着将只调用基类中的一个 __init__ 方法.

您可以通过查看 __mro__ 属性 一个类:

<预><代码>>>>D.__mro__(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)

所以从D开始,下一个类是B,接着是Cobject.从 D.__init__() 开始,super().__init__() 表达式只会调用 B.__init__(),然后因为 C.__init__() 从未调用self.c 也未设置.

您必须向类实现中添加更多 super() 调用;不带参数调用 object.__init__() 是安全的,所以只需在任何地方使用它们:

class B():def __init__(self):打印(调用 B 的 __init__")super().__init__()self.b = "B"C类():def __init__(self):打印(调用 C 的 __init__")super().__init__()self.c = "C"D类(B、C):def __init__(self):打印(调用 D 的 __init__")super().__init__()定义输出(自我):打印(self.b,self.c)

现在 B.__init__ 将调用 C.__init__,而 C.__init__ 将调用 object.__init__,并调用 D().output() 工作:

<预><代码>>>>d = D()D 的 __init__ 被调用B 的 __init__ 被调用__init__ 的 C 被调用>>>d.输出()乙丙

I am new to python. I am trying to understand super() functionality in python multiple inheritance.

class B():
    def __init__(self):
        print("__init__ of B called")
        self.b = "B"

class C():
    def __init__(self):
        print("__init__ of C called")
        self.c = "C"

class D(B, C):
    def __init__(self):
        print("__init__ of D called")
        super().__init__()

    def output(self):
        print(self.b, self.c)

d = D()
d.output()

I am getting the following error:

AttributeError: 'D' object has no attribute 'c'

解决方案

super() will find the next method in the MRO sequence. This means that only one of the __init__ methods in your base classes is going to be called.

You can inspect the MRO (the Method Resolution Order) by looking at the __mro__ attribute of a class:

>>> D.__mro__
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)

so from D, the next class is B, followed by C and object. From D.__init__(), the super().__init__() expression will only call B.__init__(), and then because C.__init__() is never called, self.c is not set either.

You'll have to add more super() calls to your class implementations; it is safe to call object.__init__() with no arguments, so just use them everywhere here:

class B():
    def __init__(self):
        print("__init__ of B called")
        super().__init__()
        self.b = "B"

class C():
    def __init__(self):
        print("__init__ of C called")
        super().__init__()
        self.c = "C"

class D(B, C):
    def __init__(self):
        print("__init__ of D called")
        super().__init__()

    def output(self):
        print(self.b, self.c)

Now B.__init__ will invoke C.__init__, and C.__init__ will call object.__init__, and calling D().output() works:

>>> d = D()
__init__ of D called
__init__ of B called
__init__ of C called
>>> d.output()
B C

这篇关于python中多重继承中的super()用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆