先来看一段代码:

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. Restful风格接口浅析

    为什么使用RESTful1.JSP技术可以让我们在页面中嵌入Java代码,但是这样的技术实际上限制了我们的开发效率,因为需要我们Java工程师将html转换为jsp页面,并写一些脚本代码,或者前端代码 ...

  2. boost system

    boost::system::error_code is the most basic class in Boost.System; it represents operating system-sp ...

  3. 知道一个数组某个index对应的值 不知道下标的情况下删除该值

    for (index,item) in Arr.enumerated() { if item == item { Arr.remove(at: index) } } 更好的方法是用数组的filter尾 ...

  4. window使用

    运行命令 regedit #注册表编译器 firewall.cpl #打开防火墙配置 powershell #打开powershell control #打开控制面版 cnpa.cpl #打开网络设置 ...

  5. JS基础入门篇(六)— 数据类型

    1.数据类型 数据类型:我感觉就是对数据的种类进行分类.就好比把人分为儿童,青少年,中年,老年一样. 基础数据类型: Number(数字),String(字符串),Null(空),Undefined( ...

  6. js中的$符

    js中的$代表什么意思呢? 首先js的作用是什么呢?是修饰网页动态内容的.那么修饰就需要定位主题,比如你把html比喻一个美女,让她唱一首歌.那么首先你要定位出是你想让哪个美女唱歌,通常我们用id来定 ...

  7. 课下选作Main dc

    一.中后缀定义: 中缀表达式:我们平时写的数学表达式一般为中缀表达式,如"5+2(3(3-12+1))",直接拿中缀表达式直接让计算机计算表达式的结果并不能做到. 后缀表达式:把中 ...

  8. Git 提交的正确姿势

    Git 提交的正确姿势:Commit message 编写指南 SCOP范围 middleware core config plugin test type范围 Git 每次提交代码,都要写 Comm ...

  9. xcodebuild自动打包上传到蒲公英的shell脚本

    注意: ExportOptions.plist (包含了证书相关信息) 该plist 文件可以通过xcode手动导出ipa之后获取到, 区分appstore 和 development的情况 #! / ...

  10. datastudion 资源导入python包,编写模块

    学习文档,不懂再问. https://help.aliyun.com/document_detail/74423.html?spm=a2c4g.11186623.6.688.72635debHqgkV ...