一、python中类的继承
1.1 单继承

在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

1.2 help(super) 看下super有些什么
>>> 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.3 super的使用

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

单继承下乍看没区别,的确。可是当考虑到多继承时

二、多继承
2.1 多继承下深度为先
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被执行很多遍。

2.2、super继承 广度为先

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()继承,搜索广度为先的更多相关文章

  1. python中使用多继承

    python中使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承,也叫菱形继承问题)等 MRO MRO即method resolution order,用于判断子类调用的属性来自于哪个父类.在 ...

  2. Python中的单继承与多继承实例分析

    Python中的单继承与多继承实例分析 本文实例讲述了Python中的单继承与多继承.分享给大家供大家参考,具体如下: 单继承 一.介绍 Python 同样支持类的继承,如果一种语言不支持继承,类就没 ...

  3. 第7.22节 Python中使用super调用父类的方法

    第7.22节 Python中使用super调用父类的方法 前面章节很多地方都引入了super方法,这个方法就是访问超类这个类对象的.由于super方法的特殊性,本节单独谈一谈super方法. 一.su ...

  4. Python中的super函数,你熟吗?

    摘要:经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么? 本文分享自华为云社区<Python中的super函数怎么学,怎么解 ...

  5. python中的super( test, self).__init__()

    python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...

  6. python中的super是什么?

    技术背景 python中的super,名为超类,可以简单的理解为执行父类的__init__函数.由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题.那么本文 ...

  7. python中的super()是什么?

    技术场景:python中的super,名为超类,可以简单的理解为执行父类的__init__函数.由于在python中不论是一对一的继承,还是一子类继承多个父类,都会涉及到执行的先后顺序的问题.那么本文 ...

  8. Python中的super()用法

    Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this,比如:C#),用于传递对象本身,而在调用的时候则不 必显式传递,系统会自动传递. 今天我们介绍的主角是su ...

  9. Python中的super().__init__()

    Python里的super().init()有什么用? 简单的说super().__init__(),就是继承父类的init方法,同样可以使用super()去继承其他方法. 下面是三种不同的继承.调用 ...

随机推荐

  1. Gson解析泛型

    1.简单对象我们传入对象Class来将JSON字符串转为对象 private static <T> T fromJson(String result, Class<T> cla ...

  2. python3基础知识梳理

    一.数据类型 1.数字 int(整型) long(长整型) float(浮点型) complex(复数) 2.布尔值 True或False 3.字符串   二.变量 变量命名规则: 变量名只能是 字母 ...

  3. 多个iframe中根据src获取特定iframe并执行操作

    多个iframe中根据src获取特定iframe并执行操作 前言:在项目中做一个批量编辑工单时需要在一大堆的iframe中的某一个iframe里边再用模态框的形式显示编辑区域,然后再在模态框里边加入i ...

  4. VMware 15 pro虚拟机

    VMware虚拟机都到VMware Workstation 15 Pro,真快,VMware 14还没用好

  5. react & vue 项目创建的方式

    创建reactApp的几种方式: create-react-app filename  适用于npm6及以下. npm init react-app filename 适用于npm6以上. npx c ...

  6. Linux 小知识翻译 - 「BitTorrent」

    这次聊聊「BitTorrent」. (也就是下片子的 BT) BitTorrent是文件传输的一种.它可以在服务端负担很小的情况下高速的传输文件. BitTorrent最大的特点就是服务端可以将文件的 ...

  7. ionic app开发遇到的问题

    :processDebugResources FAILED FAILURE: Build failed with an exception. * What went wrong: Execution ...

  8. 【Linux基础】crontab定时命令详解

    周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“crontab”,是 ...

  9. 使用readAsDataURL方法预览图片

    使用FileReader接口的readAsDataURL方法实现图片的预览. 在FileReader出现之前,前端的图片预览是这样实现的:把本地图片上传到服务器,服务器把图片地址返回,并把它替换到图片 ...

  10. skip-thought vector 实现Sentence2vector

    1.常见文本相似度计算方法 常见的短文本相似度计算方法目前有很多中,但这些短文本相似度计算方法都只是提取了短文本中的浅层特征,而无法提取短文本中的深层特征.目前常见的文本相似度计算方法有: 1)简单共 ...