Python的多重继承:选择要调用的super() [英] Python's Multiple Inheritance: Picking which super() to call

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

问题描述

在Python中,如何选择要调用的Parent方法?假设我要调用父ASDF2的__init__方法.好像我必须在super()中指定 ASDF1 .如果要调用ASDF3的__init__,则必须指定 ASDF2 ?!

In Python, how do I pick which Parent's method to call? Say I want to call the parent ASDF2's __init__ method. Seems like I have to specify ASDF1 in the super()..? And if I want to call ASDF3's __init__, then I must specify ASDF2?!

>>> class ASDF(ASDF1, ASDF2, ASDF3):
    def __init__(self):
        super(ASDF1, self).__init__()


>>> ASDF()
ASDF2's __init__ happened
>>> class ASDF(ASDF1, ASDF2, ASDF3):
    def __init__(self):
        super(ASDF2, self).__init__()


>>> ASDF()
ASDF3's __init__ happened

对我来说似乎很傻.我在做什么错了?

Seems bonkers to me. What am I doing wrong?

推荐答案

super() 用于.超级基本上是按照特定顺序选择一个(或全部)父母.如果您只想调用单亲方法,请执行以下操作

That's not what super() is for. Super basically picks one (or all) of its parents in a specific order. If you only want to call a single parent's method, do this

class ASDF(ASDF1, ASDF2, ASDF3):
    def __init__(self):
        ASDF2.__init__(self)

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

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