理解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 ...
随机推荐
- sql语句中判断空值的函数
COALESCE()函数 主流数据库系统都支持COALESCE()函数,这个函数主要用来进行空值处理,其参数格式如下: COALESCE ( expression,value1,value2……,v ...
- boost phoenix
In functional programming, functions are objects and can be processed like objects. With Boost.Phoen ...
- webpack 图片文件处理loader
目录结构: 引用图片: body { /*background: red;*/ /*background: url("../img/test2.jpg"); 小图片*/ backg ...
- jenkins部署github项目持续集成
一.先介绍正向代理和反向代理 正向代理 反向代理 二.安装反响代理得到固定域名 http://www.xiaomiqiu.cn/ 三.Jenkins与Github集成 配置前要求: 1.Jenkins ...
- 替换OSD操作的优化与分析
http://www.zphj1987.com/2016/09/19/%E6%9B%BF%E6%8D%A2OSD%E6%93%8D%E4%BD%9C%E7%9A%84%E4%BC%98%E5%8C%9 ...
- [CSP-S模拟测试]:虎(DFS+贪心)
题目传送门(内部题15) 输入格式 第一行一个整数$n$,代表点数接下来$n-1$行,每行三个数$x,y,z$,代表点$i$与$x$之间有一条边,若$y$为$0$代表初始为白色,否则为黑色,若$z$为 ...
- ORACLE基本用法及常用命令
切换ORACLE用户 su - oracle ---------------------------- 重启数据库 sqlplus sys / as sysdba shutdown immediate ...
- 九条命令检查Linux服务器性能
一.uptime命令 这个命令可以快速查看机器的负载情况.在Linux系统中,这些数据表示等待CPU资源的进程和阻塞在不可中断IO进程(进程状态为D)的数量.这些数据可以让我们对系统资源使用有一个宏观 ...
- exporter
何为 Prometheus Exporter? Prometheus 监控基于一个很简单的模型: 主动抓取目标的指标接口(HTTP 协议)获取监控指标, 再存储到本地或远端的时序数据库. Promet ...
- Spring 官方文档笔记---Bean
In Spring, the objects that form the backbone of your application and that are managed by the Spring ...