Python多继承问题 [英] python multiple inheritance qustion

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

问题描述

这是我从以下问题的第10个问题中复制和修改的一个访谈示例问题: https://www.codementor.io/sheena/essential-python-采访问题du107ozr6

This is an interview example question I copied and modified from question 10 of: https://www.codementor.io/sheena/essential-python-interview-questions-du107ozr6

class A(object):
    def go(self):
        print("go A go!")
    def stop(self):
        print("stop A stop!")

class B(A):
    def go(self):
        super(B, self).go()
        print("go B go!")

class C(A):
    def go(self):
        super(C, self).go()
        print("go C go!")
    def stop(self):
        super(C, self).stop()
        print("stop C stop!")

class D(B,C):
    def go(self):
        super(D, self).go()
        print("go D go!")
    def stop(self):
        super(D, self).stop()
        print("stop D stop!")

class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
print "call b.stop()......."
b.stop()
print "call d.stop()......."
d.stop()
print "call e.stop()......."
e.stop()

答案是:

call b.stop().......
stop A stop!
call d.stop().......
stop A stop!
stop C stop!    
stop D stop!  #?why not having b.stop() which leads to "stop A stop!"
call e.stop().......
stop A stop!
stop C stop!

我知道调用b.stop()会显示"stop A stop!"因为b不会覆盖stop(),所以将从A继承stop().

I understand calling b.stop() shows "stop A stop!" because b does not override stop() so will inherit stop() from A.

但是我不明白为什么调用d.stop()仅显示A,C,D的停止,而不显示ACBD,不是MRO:D-> B-> C-> A?

But I do not understand why calling d.stop() only show stop of A,C,D, not ACBD, isn't MRO: D->B->C->A?

而且我不明白为什么调用M.MRO的e.stop()只显示A和C的停止:E-> B-> C-> A,我认为e.stop()应该继承B的停止(),因此应该先停止A停止,再停止C停止,然后再停止B停止?

and I do not understand why calling e.stop() only shows stop of A and C, based on MRO: E->B->C->A, I think e.stop() should inherit from B's stop(), so should be stop A stop, stop C stop, then stop B stop?

我一定对某事有误解.关于超级我猜.

I must have misunderstand sth. about super I guess.

推荐答案

我知道调用b.stop()会显示"stop A stop!"因为b不 覆盖stop(),这样将从A继承stop().

I understand calling b.stop() shows "stop A stop!" because b does not override stop() so will inherit stop() from A.

但是我不明白为什么调用d.stop()仅显示A,C,D的停止, 不是ACBD,不是MRO:D-> B-> C-> A?

But I do not understand why calling d.stop() only show stop of A,C,D, not ACBD, isn't MRO: D->B->C->A?

B从A继承了stop,但这意味着当您尝试访问B.stopsome_B_instance.stop时,属性搜索将在B.__dict__中查找后通过A.__dict__找到该方法.它不会将方法直接放在B类中.

B inherits stop from A, but what that means is that when you try to access B.stop or some_B_instance.stop, the attribute search will find the method through A.__dict__ after looking in B.__dict__. It doesn't put the method in the B class directly.

super遵循D实例的MRO时,类B紧随D之后,但super此时仅对查看B本身感兴趣,而不对B的祖先感兴趣.它在B.__dict__中查找stop条目,而无需考虑继承的方法.继承的方法将在搜索中稍后处理,当super到达那些方法所继承的类时.

When super follows the MRO of a D instance, class B comes after D, but super is only interested in looking at B itself at this point, not B's ancestors. It looks in B.__dict__ for a stop entry, without considering inherited methods; inherited methods will be handled later in the search, when super reaches the classes those methods are inherited from.

由于继承方法实际上并未将其放入B.__dict__中,因此superB中未找到stop.

Since inheriting methods doesn't actually put them in B.__dict__, super doesn't find stop in B.

这篇关于Python多继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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