浅析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__)
(<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函数

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,551评论 0 17
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,788评论 0 9
  • 一、定义策略类场景:文章的修改和删除操作 二、编写策略类 三、注册策略类和模型关联AuthServiceProvi...
    小慕先森阅读 229评论 0 0