超级混乱的python多重继承super() [英] super confusing python multiple inheritance super()

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

问题描述

我正在玩python中的多重继承,我遇到了一个我无法理解它是如何发生的情况。

I was playing around with the multiple inheritance in python and I come a cross a situation that I can't understand how it happen.

这是继承布局:

    A       F
  /   \     |
 B     C    |   
  \    |   /
    \  |  /   
       D

每个人都熟悉的ABCD钻石。
加上额外的F级别我把它扔进去玩。

The ABCD diamond that everyone familiar with. Plus an extra class "F" I throw it in for fun.

这是代码:

class A(object):
    def foo(self, call_from):
        print "foo from A, call from %s" % call_from
        super(A, self).foo("A")

class B(A):
    def foo(self, call_from):
        print "foo from B, call from %s" % call_from
        super(B, self).foo("B")

class C(A):
    def foo(self, call_from):
        print "foo from C, call from %s" % call_from
        super(C, self).foo("C")

class F(object):
    def foo(self, call_from):
        print "foo from F, call from %s" % call_from

class D(B, C, F):
    def foo(self):
        print "foo from D"
        super(D, self).foo("D")

输出:

>>> d = D()
>>> d.foo()
foo from D
foo from B, call from D
foo from C, call from B
foo from A, call from C
foo from F, call from A

方法解析顺序:

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




  • 来自C的foo,来自B 的呼叫,而不是来自C的 foo,来自D的呼叫

  • 来自F的foo,来自A的调用只是简单地把我扔掉......

    • foo from C, call from B instead of foo from C, call from D
    • foo from F, call from A just simply throw me off...
    • 似乎 super()根据方法解析顺序链接并忽略类之间的关系,但我不确定。

      It seems like the super() are chained up according to the method resolution order and ignore the relationship between classes, but I not sure.

      有人能指出我正确的方向来理解这种行为吗?

      Can someone point me to the right direction to understand this behavior?

      请记住我正试图理解语言本身。不试图解决实际问题。所以我没有这个用例。但如果有人可以指出一个用例,那将是很好的:)

      Please keep in mind that I'm trying to understand the language itself. Not trying to solve a practical problem. So I don't have an use case for this. But it will be nice if someone can point out an use case :)


      更新:

      UPDATE:

      总结一下 - super()只是让你知道mro上的呼叫基础旁边是什么。父母没有必要。虽然mro基于继承层次构建,但mro本身不是继承层次结构。

      To summarize - super() simply let you know what is next to call base on the mro. It is not necessary the parent. While mro built base on the inheritance hierarchy, mro itself is not the inheritance hierarchy.


      推荐答案

      super()的重点是遵循方法解析顺序。这就是为什么你告诉它自己的课程,而不是你的父课。程序员很难预测接下来会调用哪个类,所以你让 super()来处理它。

      The whole point of super() is to follow the method resolution order. That's why you tell it your own class, not your parent class. It's hard for the programmer to predict which class will be invoked next, so you let super() take care of it.

      你已经从D调过B了,那你怎么能从D调用C? D.foo()只能调用另一个foo(),因为那里只有一个函数调用。它将是一个线性调用链,因此类必须线性化,这就是方法解析顺序所做的。

      You already had B called from D, so how could you then get C called from D? D.foo() can only call one other foo(), because you only have one function call there. It's going to be a linear chain of calls, so the classes have to be linearized, that's what the method resolution order does.

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

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