先来看一段代码:

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. 修改host,访问指定服务器

    途径: 1.hosts文件修改 以Windows文件为例:进入system32/drivers/etc/hosts 或者用一些软件比如switchhost等来进行修改 2.通过抓包工具修改 因为本人是 ...

  2. MYSQL5.7.9改密码相关设置

    Centos7上,对MySQL5.7开启远程连接. 1.修改/etc/my.cnf [mysqld] validate_password=off 2.命令行进入mysql use mysql; GRA ...

  3. boost location-independent times

    The class boost::posix_time::ptime defindes a location-independent time. It uses the type boost::gre ...

  4. 阿里下一代云分析型数据库AnalyticDB入选Forrester云化数仓象限

    前言 近期, 全球权威IT咨询机构Forrester发布"The Forrester Wave: CloudData Warehouse Q4 2018"研究报告,阿里巴巴分析型数 ...

  5. Linux Bash Shell快速入门 (三)

    forfor 循环结构与 C 语言中有所不同,在 BASH 中 for 循环的基本结构是: for $var in dostatmentsdone 其中 $var 是循环控制变量, 是 $var 需要 ...

  6. 查看SQL Server被锁的表以及如何解锁【转】

    锁定数据库的一个表的区别  SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除  SELECT * FROM table WITH (TABLO ...

  7. python 中的__str__ 和__repr__方法

    看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...

  8. 【原】webpack--plugins,主要解释plugins干了啥

    其实呢,plugins是增强webpack的功能, 插件用于bundle文件的优化,资源管理和环境变量的注入, 可以理解为任何loaders不能做的事让它来做, 作用于整个构建过程. 常见的plugi ...

  9. leetcode-解题记录 206. 反转链表

    题目 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可 ...

  10. T1218:取石子游戏

    [题目描述] 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍,最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和 ...