一、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. 阿里云ECS Ubuntu16.0 安装 uwsgi 失败解决方案

    Ubuntu安装包时报错 E:Unable to locate package xxx(如:python3-pip) 一般新安装Ubuntu后需要先更新软件源: apt-get update apt- ...

  2. JavaScript -- 时光流逝(二):js中数组的方法

    JavaScript -- 知识点回顾篇(二):js中数组的方法 1. 数组 (1)定义数组,数组赋值 <script type="text/javascript"> ...

  3. May 27. 2018 Week 22nd Sunday

    All things come to those who wait. 苍天不负有心人. It is said that those who are patient can see what their ...

  4. [matlab] 1.拟合

    x = [1 2 3 4 5 6 7 8 9 ]; y = [9 7 6 3 -1 2 5 7 20]; p=polyfit(x,y,3); %数字代表拟合函数的阶数 xi=0:0.01:10; yi ...

  5. WINDOWS下nginx实现本地支持的图片服务器反向代理

    最近在学习nginx的反向代理和负载均衡的运用时,觉得挺简单的,但是还是想写一篇博客用于记录自己的学习过程. 好的,我们进入正题 Nginx(engine x) 是一个高性能的HTTP和反向代理服务器 ...

  6. centos7修改系统语言为简体中文

    centos7修改系统语言为简体中文 说明 自己装系统时一般都可以自定义选择系统语言.可是云端服务器一般都是安装好的镜像,默认系统语言为英文,对于初学者可能还会有搞不懂的计算机词汇.这里简单说一下ce ...

  7. 【css3】nth-child

    nth-child含义 :nth-child(an+b) 这个 CSS 伪类匹配文档树中在其之前具有 an+b-1 个兄弟节点的元素,其中 n 为正值或零值. 简单点说就是,这个选择器匹配那些在同系列 ...

  8. pytorch visdom可视化工具学习—1—详细使用-1—基本使用函数

    使用教程,参考: https://github.com/facebookresearch/visdom https://www.pytorchtutorial.com/using-visdom-for ...

  9. ubuntu查看安装的pytorch/cuda版本

    使用命令: user@home:~$ python Python |Anaconda custom (-bit)| ( , ::) [GCC ] on linux Type "help&qu ...

  10. Python 使用 distutils 工具安装的扩展包的卸载

    Python 编写完扩展包并 build 好后,可以采用 $ sudo ./setup.py install 安装.采用这种方式安装的扩展包,可以使用 pip list 查看到,但不能直接使用 pip ...