python 中的super()继承,搜索广度为先
在python 中我们可以这样来定义一个类:及继承它的子类
class Father:
def __init__(self, mes): #1 父类的init构造方法
print('From Father. form {}'.format(mes))
class Child(Father):
def __init__(self, message, personal): #2 子类的构造方法
Father.__init__(self, message) #3 继承父类构造方法
print("I'm child. From {}".format(personal))
c = Child('儿子', '拼爹')
#执行结果为
From Father. form 儿子 #4
I'm child. From 拼爹 #5
在这个列子中,子类 Child 继承父类 Father,并在#2 自己的构造方法#3处中继承父类的构造方法,先执行完#3处继承父类#1的构造放方法得到结果#4,然后继续执行自身构造方法输出#5
>>> help(super)
Help on class super in module builtins:
class super(object)
| super() -> same as super(__class__, <first argument>)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
| Typical use to call a cooperative superclass method:
| class C(B):
| def meth(self, arg):
| super().meth(arg)
| This works for class methods too:
| class C(B):
| @classmethod
| def cmeth(cls, arg):
| super().cmeth(arg)
|
| Methods defined here:
|
#...........后面还有很多,没什么好讲的,这儿就够了
1. super() #另一个写法 super('class_name', self)
2. super(type)
3. super(type, obj)
4. super(type, type2)
推荐第一种写法,方便
class A:
def __init__(self):
print("From A")
class B(A):
def __init__(self):
super().__init__() #1 这儿还有个写法super(B, self).__init__()
print("From B")
b = B()
#结果为
From A
From B
单继承下乍看没区别,的确。可是当考虑到多继承时
class A:
def __init__(self):
print("From A")
print("END A")
class B(A):
def __init__(self):
print("From B")
A.__init__(self)
print("END B")
class C(A):
def __init__(self):
print("From C")
A.__init__(self)
print("END C")
class D(A):
def __init__(self):
print("From D")
A.__init__(self)
print("END D")
class E(B, C, D):
def __init__(self):
print("From E")
B.__init__(self)
C.__init__(self)
D.__init__(self)
print("END E")
E()
#结果为
From E
From B
From A
END A
END B
From C
From A
END A
END C
From D
From A
END A
END D
END E
首先可以看到,E当中继承顺序是 B、C、D,
然后搜索顺序则为先到第一个继承的E-->B-->A -->C-->A -->D-->A 深度为先。然后结果大家也看到了,这样的继承方式会导致A被执行很多遍。
super的使用方式有两种:
1. super()__init__()
2. super(类名, self).__init__()
class A:
def __init__(self):
print("From A")
print("END A")
class B(A):
def __init__(self):
print("From B")
super().__init__()
print("END B")
class C(A):
def __init__(self):
print("From C")
super(C, self).__init__()
print("END C")
class D(A):
def __init__(self):
print("From D")
super(D, self).__init__()
print("END D")
class E(B, C, D):
def __init__(self):
print("From E")
super().__init__()
print("END E")
E() #1 继承顺序
print(E.__mro__) #2 查看super类里面维护的继承顺序
#-结果为:
From E
From B
From C
From D
From A
END A
END D
END C
END B
END E
(<class '__main__.E'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>) #3
可以看到#1处E(B, C, D)的继承查找顺序就为 E-->B-->C-->D-->A 通过#2处
E.__mro__属性也可以看到,super自己维护的顺序为#3最后的列表 E-->B-->C-->D-->A--object(所有新式类的基类)
是一样的。因为它就是按照__mro__来寻找的
python 中的super()继承,搜索广度为先的更多相关文章
- python中使用多继承
python中使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承,也叫菱形继承问题)等 MRO MRO即method resolution order,用于判断子类调用的属性来自于哪个父类.在 ...
- Python中的单继承与多继承实例分析
Python中的单继承与多继承实例分析 本文实例讲述了Python中的单继承与多继承.分享给大家供大家参考,具体如下: 单继承 一.介绍 Python 同样支持类的继承,如果一种语言不支持继承,类就没 ...
- 第7.22节 Python中使用super调用父类的方法
第7.22节 Python中使用super调用父类的方法 前面章节很多地方都引入了super方法,这个方法就是访问超类这个类对象的.由于super方法的特殊性,本节单独谈一谈super方法. 一.su ...
- Python中的super函数,你熟吗?
摘要:经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么? 本文分享自华为云社区<Python中的super函数怎么学,怎么解 ...
- python中的super( test, self).__init__()
python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...
- python中的super是什么?
技术背景 python中的super,名为超类,可以简单的理解为执行父类的__init__函数.由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题.那么本文 ...
- python中的super()是什么?
技术场景:python中的super,名为超类,可以简单的理解为执行父类的__init__函数.由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题.那么本文 ...
- Python中的super()用法
Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this,比如:C#),用于传递对象本身,而在调用的时候则不 必显式传递,系统会自动传递. 今天我们介绍的主角是su ...
- Python中的super().__init__()
Python里的super().init()有什么用? 简单的说super().__init__(),就是继承父类的init方法,同样可以使用super()去继承其他方法. 下面是三种不同的继承.调用 ...
随机推荐
- JavaScript -- 时光流逝(四):js中的 Math 对象的属性和方法
JavaScript -- 知识点回顾篇(四):js中的 Math 对象的属性和方法 1. Math 对象的属性 (1) E :返回算术常量 e,即自然对数的底数(约等于2.718). (2) LN2 ...
- 4.4Python数据处理篇之Matplotlib系列(四)---plt.bar()与plt.barh条形图
目录 目录 前言 (一)竖值条形图 (二)水平条形图 1.使用bar()绘制: 2.使用barh()绘制: (三)复杂的条形图 1.并列条形图: 2.叠加条形图: 3.添加图例于数据标签的条形图: 目 ...
- Docker: dockerfile常用关键字
Dockerfile指令 Dockfile执行和shell命令一行,一行一行执行- 写Dockerfile注意点: 1. 尽量少RUN 2. 多个命令拼接在一起 ...
- 聚类——WKFCM
聚类——认识WKFCM算法 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 参考文献:Shen H, Yang J, Wang S, et al. At ...
- MATLAB简易画图
给定一组特殊点,连线作图 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 以成绩隶属函数为例: score.m cj_x1=[ 0.1]; cj_y1= ...
- [福大软工] Z班 第6次成绩排行榜
作业要求 http://www.cnblogs.com/easteast/p/7668890.html 作业评分 本次作业从引言(5 ') . 用户场景(15 ').类图(10 ').界面原型(15 ...
- cookie保存登录的用户名和密码
用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用 用cookie保存登录的用户名和密码,当用户访问网站的时候,获取cookie的用户名和密码,通过用户 ...
- (转)Spring Boot 2 (四):使用 Docker 部署 Spring Boot
http://www.ityouknow.com/springboot/2018/03/19/spring-boot-docker.html Docker 技术发展为微服务落地提供了更加便利的环境,使 ...
- elasticSearch学习安装
资料: 1.Elasticsearch学习,请先看这一篇! https://blog.csdn.net/laoyang360/article/details/52244917 2. linux下ela ...
- php面试中的经典问题
原文:https://blog.csdn.net/ghostlv/article/details/51284745 问题一问题描述考虑下面代码: $str1 = 'yabadabadoo';$str2 ...