理解Python中的继承规则和继承顺序
先来看一段代码:
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先继承了父类 First 的def __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中的继承规则和继承顺序的更多相关文章
- 【转】你真的理解Python中MRO算法吗?
你真的理解Python中MRO算法吗? MRO(Method Resolution Order):方法解析顺序. Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多 ...
- [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式
使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...
- Python中的LEGB规则
目标 命名空间和作用域——Python从哪里查找变量名? 我们能否同时定义或使用多个对象的变量名? Python查找变量名时是按照什么顺序搜索不同的命名空间? 命名空间与作用域的介绍 命名空间 大约来 ...
- 深入理解Python中的GIL(全局解释器锁)
深入理解Python中的GIL(全局解释器锁) Python是门古老的语言,要想了解这门语言的多线程和多进程以及协程,以及明白什么时候应该用多线程,什么时候应该使用多进程或协程,我们不得不谈到的一个东 ...
- 全面理解python中self的用法
self代表类的实例,而非类. class Test: def prt(self): print(self) print(self.__class__) t = Test() t.prt() 执行结果 ...
- Python 中的继承、多态和封装
涉及问题: Python 中如何实现多继承,会有什么问题? Python 中的多态与静态方法有什么区别? 答案要点如下: Python 中的继承,就是在定义类时,在括号中声明父类,简单示例如下: cl ...
- 理解 Python 中的可变参数 *args 和 **kwargs:
默认参数: Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如: def getSum(x,y=5): print "x:", x print "y:& ...
- 深入理解Python中的yield和send
send方法和next方法唯一的区别是在执行send方法会首先把上一次挂起的yield语句的返回值通过参数设定,从而实现与生成器方法的交互. 但是需要注意,在一个生成器对象没有执行next方法之前,由 ...
- 如何理解python中的if __name__=='main'的作用
一. 一个浅显易懂的比喻 我们在学习python编程时,不可避免的会遇到if __name__=='main'这样的语句,它到底有什么作用呢? <如何简单地理解Python中的if __name ...
随机推荐
- Spring----组合注解与元注解
1.概述 1.1.Spring提供了大量的注解, 尤其是相同的注解用到各个类中,会相当的啰嗦: 1.2.元注解: 可以注解到别的注解上的注解: 组合注解: 被注解注解的注解称为 组合注解: 组合注解 ...
- excel VBA 编程
Dim cuttent_columns As Integer Dim care_repeat As Integer Private Sub Workbook_Open() Dim i As Integ ...
- 4412 i2c驱动
1.Linux主机驱动和外设驱动分离思想 外设驱动→API→主机驱动→板机逻辑--具体的i2c设备(camera,ts,eeprom等等) 2.主机驱动 根据控制器硬件手册,操作具体的寄存器,产生波形 ...
- 禅道安装--结合openldap
原文地址: https://blog.csdn.net/plei_yue/article/details/79075298 ldap结合禅道(需要神道不是开源版) https://www.cnblog ...
- (转)Docker 网络
转:https://www.cnblogs.com/allcloud/p/7150564.html 本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker ...
- 使用ajax前必须了解的知识
ajax的全称: asynchronous javascript and xml (异步的javascript和xml) ajax不是某种编程语言 是一种在无需重新加载整个页面的情况下能够更新部分网页 ...
- iPad如何恢复
iPad在有网络连接的情况下,可以通过iCloud进行恢复. 没有连接WiFi的情况下,只能通过USB连接才能恢复. ①下载最新版本的iTunes:https://support.apple.com/ ...
- 【Angular】No component factory found for ×××.
报错现象: 用modal打开某个组件页面时报错 报错:No component factory found for UpdateAuthWindowComponent. Did you add it ...
- Jenkins构建触发器的区别
Build periodically:定时进行项目构建或执行(它不care源码是否发生变化),配置如下: 0 2 * * * (每天2:00 必须build一次源码) 如果是要定时执行脚本,需要选择 ...
- Java集合的介绍
参考博客: https://blog.csdn.net/zhangqunshuai/article/details/80660974 List , Set, Map都是接口,前两个继承至Collect ...