多重继承如何与super()和不同的__init __()参数一起使用? [英] How does multiple inheritance work with the super() and different __init__() arguments?

查看:145
本文介绍了多重继承如何与super()和不同的__init __()参数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是深入研究一些更高级的python主题(至少对我来说是高级的).我现在正在阅读有关多重继承以及如何使用super()的信息.我或多或少了解超级功能的使用方式,但是(1)像这样执行超级操作怎么了?:

I'm just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) What's wrong with just doing it like this?:

class First(object):
    def __init__(self):
        print "first"

class Second(object):
    def __init__(self):
        print "second"

class Third(First, Second):
    def __init__(self):
        First.__init__(self)
        Second.__init__(self)
        print "that's it"

在super()上, Andrew Kuchlings关于Python Warts的论文说:

On to super(), Andrew Kuchlings paper on Python Warts says:

当Derived类继承时,super()的

用法也将是正确的 来自多个基类,并且部分或全部具有 init 方法

usage of super() will also be correct when the Derived class inherits from multiple base classes and some or all of them have init methods

因此,我将以上示例重写如下:

So I rewrote the example above as follows:

class First(object):
    def __init__(self):
        print "first"

class Second(object):
    def __init__(self):
        print "second"

class Third(First, Second):
    def __init__(self):
        super(Third, self).__init__(self)
        print "that's it"

但是,此操作仅运行它可以找到的第一个 init ,位于First中. (2)能否使用super()来同时运行FirstSecond的init,如果是的话,怎么办?只需先运行两次super(Third, self).__init__(self)即可. init ()两次.

This however, only runs the first init it can find, which is in First. (2) Can super() be used to run both the init's from First and Second, and if so, how? Running super(Third, self).__init__(self) twice just runs First.init() twice..

要增加一些混乱.如果继承的类的 init ()函数采用不同的参数怎么办?例如,如果我有这样的东西怎么办:

To add some more confusion. What if the inherited classes' init() functions take different arguments. For example, what if I had something like this:

class First(object):
    def __init__(self, x):
        print "first"

class Second(object):
    def __init__(self, y, z):
        print "second"

class Third(First, Second):
    def __init__(self, x, y, z):
        First.__init__(self, x)
        Second.__init__(self, y, z)
        print "that's it"

(3)如何使用super()为不同的继承类init函数提供相关的参数?

欢迎所有提示!

ps.由于我有几个问题,因此我将它们加粗并编号..

ps. Since I have several questions I made them bold and numbered them..

推荐答案

对于问题2,您需要在每个类中调用super:

For question 2, you need to call super in each class:

class First(object):
    def __init__(self):
        super(First, self).__init__()
        print "first"

class Second(object):
    def __init__(self):
        super(Second, self).__init__()
        print "second"

class Third(First, Second):
    def __init__(self):
        super(Third, self).__init__()
        print "that's it"

对于问题3,这无法完成,您的方法需要具有相同的签名.但是您可以忽略父句中的某些参数,也可以使用关键字参数.

For question 3, that can't be done, your method needs to have the same signature. But you could just ignore some parameters in the parent clases or use keywords arguments.

这篇关于多重继承如何与super()和不同的__init __()参数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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