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()去继承其他方法. 下面是三种不同的继承.调用 ...
随机推荐
- LeetCode算法题-Power of Four(Java实现-六种解法)
这是悦乐书的第205次更新,第216篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第72题(顺位题号是342).给定一个整数(带符号的32位),写一个函数来检查它是否为4 ...
- Lua-pb 升级到Lua5.3
项目lua库升级到5.3版本后,最头疼的就是原先的一些第三方库原先只是基于lua5.1设计的,比如protobuff 相关的的. 之前项目引入Lua-pb 实现protobuf的解析和使用,但是这个库 ...
- C# 基础知识之 Unix 时间戳转换
unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒. /// 时间戳转为C#格式时间 private DateTime GetTime(string timeSt ...
- 前端使用node.js+express+mockjs+mysql实现简单服务端,2种方式模拟数据返回
今天,我教大家来搭建一个简单服务端 参考文章: https://www.jianshu.com/p/cb89d9ac635e https://www.cnblogs.com/jj-notes/p/66 ...
- 生成.project、.classpath文件
新导出的项目如果没有.project文件,是不能导入到工具中的,需要执行maven命令生产,在进入工作空间下面,按住shift再点击鼠标右键,点击选择在此处打开命令窗口. 然后输入对应的maven命令 ...
- c# 行转列动态赋值给layui
数据库存储格式 期望前端显示样式 以下是代码: (1)控制器: [HttpGet("SocialImportLedgerInfo")] public ResultData GetS ...
- 为什么swift是面向协议的编程--对面向对象机制的改进
主要目标是提供抽象能力和解决值类型的多态问题 Actually, Abrahams says, those are all attributes of types, and classes are j ...
- 属性复制方法,当属性名字不一致时候可以传入匹配的Map
/** * @param src * @param dest * @param filedMapping the diffrent fieldName mapping,key is src filen ...
- 【转】bios与CMOS有什么区别
正确的解释应该是: BIOS是软件.是程序! CMOS是芯片.是硬件! 实际上我们是通过BIOS这个程序,去设置CMOS里的参数的. CMOS是一块芯片,集成在主板上,里面保存着重要的开机参数,而保存 ...
- Python:Day28 同步锁
同步锁: Python不是有一把锁了吗?为什么还要加锁? Python解释器的GIL的作用是同一时刻只有一个线程被CPU执行,而同步锁的作用同一时刻只有一个线程对锁定代码块操作 如果不加锁,当多个线程 ...