阅读各种框架的源码时,各种super满天飞,嗯,所以又再次下决心搞懂他
先来一个比较简单的例子
class Root(object):
def __init__(self):
self.name="zliang"
def call(self):
print self.name
a=Root()
a.call()
[out] : zliang
再定义一个继承它的类:
class B(Root):
def call(self):
super(B,self).call()
b=B()
b.call()
[out] : zliang
咦,好像就是执行他的父类里面对应的call函数耶,这时候我感觉很开心了,然而。。。
In [1]: class Root(object):
...: def __init__(self):
...: print("this is Root")
...:
...: class B(Root):
...: def __init__(self):
...: print("enter B")
...: super(B, self).__init__()
...: print("leave B")
...:
...: class C(Root):
...: def __init__(self):
...: print("enter C")
...: super(C, self).__init__()
...: print("leave C")
...:
...: class D(B, C):
...: pass
开始懵逼了:
In [3]: D()
#至于In[2]哪去了,还用问么,当然是打错了
enter B
enter C
this is Root
leave C
leave B
有没有注意到,如果真的只是简单的继承父类的话,为什么会在enter B
之后会出现 enter C
而不是直接就是 this is Root
,
print("enter B")
super(B, self).__init__()
print("leave B")
下面我们来慢慢分析:
In [9]: print(d.__class__.__mro__)
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Root'>, <type 'object'>)
是的,你没看错,这个关系是这样的D->B->C->Root。也就是说执行了print ("enter B")
之后,再执行super(B, self).__init__()
,并不会跳到Root的__init__
,而是会转入class C
中的__init__()
,执行print ("enter C")
,然后再进入Root的__init__()
,执行print("this is Root")
,然后再依次返回,应该是栈的原理。
那这么说,先执行print ("enter B")
和print ("enter C")
就和
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.Root'>, <type 'object'>)
这个有关咯,是不是呢,我们可以简单的验证一下:
n [10]: class E(C,B):
...: pass
...:
In [12]: e=E()
enter C
enter B
this is Root
leave B
leave C
In [14]: print(e.__class__.__mro__)
(<class '__main__.E'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.Root'>, <type 'object'>)
果不其然,class E(C,B)
的.__class__.__mro_
就变成了D->C->B->Root
好了,到这里对super的用法应该也了解的差不多了,详尽的解释可以参考这篇博客,我就不赘述了:
Python-理解super函数