pythonsuper原理_浅析python super

阅读各种框架的源码时,各种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__)

(, , , , )

是的,你没看错,这个关系是这样的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")就和

(, , , , )

这个有关咯,是不是呢,我们可以简单的验证一下:

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 E(C,B)的.__class__.__mro_就变成了D->C->B->Root

好了,到这里对super的用法应该也了解的差不多了,详尽的解释可以参考这篇博客,我就不赘述了:

Python-理解super函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值