先来看一段代码:

class First(object):
def __init__(self):
print ("first") class Second(object):
def __init__(self):
print ("second") class Third(object):
def __init__(self):
print ("third") class Forth(object):
def __init__(self):
print ("forth") class Five(First, Second, Third, Forth):
def __init__(self):
print ("that's it") a = Five()

这段代码的输出结果是:

that's it

也就是说,在class Five中的def__init__(self)override了父类(classes: First, Second, Third, Forth)的def__init__(self)

class First(object):
def __init__(self):
print ("first") class Second(object):
def __init__(self):
print ("second") class Third(object):
def __init__(self):
print ("third") class Forth(object):
def __init__(self):
print ("forth") class Five(First, Second, Third, Forth):
def __init__(self):
super().__init()__
print ("that's it") a = Five()

输出结果是:

first
that's it

也就是说,class Five先继承了父类 Firstdef __init__(self),然后执行自己重新定义的def __init__(self)

如果在所有父类中也使用super().__init__,事情变得有趣:

class First(object):
def __init__(self):
super().__init__()
print ("first") class Second(object):
def __init__(self):
super().__init__()
print ("second") class Third(object):
def __init__(self):
super().__init__()
print ("third") class Forth(object):
def __init__(self):
super().__init__()
print ("forth") class Five(First, Second, Third, Forth):
def __init__(self):
super().__init__()
print ("that's it") a = Five()

这段代码的输出结果是:

forth
third
second
first
that's it

也就是说,如果在父类中都加入super()class Five执行父类中def __init__()的顺序是class Forth -> class Third -> class Second -> class First

也就是说,class Five在继承了class First后,不会马上停止并执行class FIrst中的def __init__(),而是继续往下搜寻,直到最后。

理解Python中的继承规则和继承顺序的更多相关文章

  1. 【转】你真的理解Python中MRO算法吗?

    你真的理解Python中MRO算法吗? MRO(Method Resolution Order):方法解析顺序. Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多 ...

  2. [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式

    使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...

  3. Python中的LEGB规则

    目标 命名空间和作用域——Python从哪里查找变量名? 我们能否同时定义或使用多个对象的变量名? Python查找变量名时是按照什么顺序搜索不同的命名空间? 命名空间与作用域的介绍 命名空间 大约来 ...

  4. 深入理解Python中的GIL(全局解释器锁)

    深入理解Python中的GIL(全局解释器锁) Python是门古老的语言,要想了解这门语言的多线程和多进程以及协程,以及明白什么时候应该用多线程,什么时候应该使用多进程或协程,我们不得不谈到的一个东 ...

  5. 全面理解python中self的用法

    self代表类的实例,而非类. class Test: def prt(self): print(self) print(self.__class__) t = Test() t.prt() 执行结果 ...

  6. Python 中的继承、多态和封装

    涉及问题: Python 中如何实现多继承,会有什么问题? Python 中的多态与静态方法有什么区别? 答案要点如下: Python 中的继承,就是在定义类时,在括号中声明父类,简单示例如下: cl ...

  7. 理解 Python 中的可变参数 *args 和 **kwargs:

    默认参数:  Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def getSum(x,y=5): print "x:", x print "y:& ...

  8. 深入理解Python中的yield和send

    send方法和next方法唯一的区别是在执行send方法会首先把上一次挂起的yield语句的返回值通过参数设定,从而实现与生成器方法的交互. 但是需要注意,在一个生成器对象没有执行next方法之前,由 ...

  9. 如何理解python中的if __name__=='main'的作用

    一. 一个浅显易懂的比喻 我们在学习python编程时,不可避免的会遇到if __name__=='main'这样的语句,它到底有什么作用呢? <如何简单地理解Python中的if __name ...

随机推荐

  1. Sublime Text 注册及使用相关

    sublime text3 注册码 2019-07-01 注册码可以直接用 地址: 2019-07-01 亲测可用 2019-07-18 亲测可用 -– BEGIN LICENSE -– Die So ...

  2. 51nod 1714:B君的游戏(博弈 sg打表)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...

  3. CSS中的flex布局

    1.flex 布局的概念 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局,行内元素也可以通过 ...

  4. JSON对象及方法

    1.JSON JSON 包括 JSON 字符串和 JSON 对象.JSON 通常用于与服务端交换数据,在给服务器接收和发送数据时用的都是字符串,可以是 JSON 字符串或者一般的键值对字符串.把Jav ...

  5. NOIp 图论算法专题总结 (3):网络流 & 二分图 简明讲义

    系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 网络流 概念 1 容量网络(capacity network)是一个有向图,图的 ...

  6. git filter-branch

    https://github.com/git-for-windows/git/issues/2206 https://git-scm.com/docs/git-filter-branch The -- ...

  7. tomcat正常启动输入localhost:8080显示404错误

    找了半天才解决. 看这个贴子: https://www.cnblogs.com/lovelanglangyou/p/7410937.html 简而言之: 需要修改eclipse中的server配置,e ...

  8. day 109结算中心.

    from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey,Generi ...

  9. day106 支付功能与优惠券功能 contentype

    https://blog.csdn.net/Ayhan_huang/article/details/78626957 一.ContenType from django.db import models ...

  10. cabal替代脚本

    由于网络原因,直接使用cabal update不成功,只能自己写脚本直接从网上拖包下来,自己安装. 但是这样做的缺点是需要手动处理dependency,当然,也可以把脚本写的复杂些,自动来处理depe ...