python 中的super()继承,搜索广度为先

时间:2022-09-12 16:48:10
一、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&period;22节 Python中使用super调用父类的方法

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

  4. python中的super&lpar; test&comma; self&rpar;&period;&lowbar;&lowbar;init&lowbar;&lowbar;&lpar;&rpar;

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

  5. Python中的super&lpar;&rpar;用法

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

  6. python中的super怎么用&quest;

    面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...

  7. 第11&period;23节 Python 中re模块的搜索替换功能:sub及subn函数

    一. 引言 在<第11.3节 Python正则表达式搜索支持函数search.match.fullmatch.findall.finditer>重点介绍了几个搜索函数,除了搜索,re模块也 ...

  8. 认识python中的super函数

    需求分析 在类继承中,存在这么一种情况: class Human(object): def Move(self): print("我会走路...") class Man(Human ...

  9. python中的多继承

    python和C++一样,支持多继承.概念虽然容易,但是困难的工作是如果子类调用一个自身没有定义的属性,它是按照何种顺序去到父类寻找呢,尤其是众多父类中有多个都包含该同名属性. class P1 #( ...

随机推荐

  1. Flexbox制作CSS布局实现水平垂直居中

    Flexbox实现一个div元素在body页面中水平垂直居中: <!DOCTYPE html><html lang="en"><head>  & ...

  2. json字符串的拼接,并转换为json对象

    <html> <head> <script> var qianzhui = "cc"; var test1=""; func ...

  3. Productivity Power Tools 是微软官方推出的 Visual Studio 扩展

    Productivity Power Tools 是微软官方推出的 Visual Studio 扩展 免费的精品: Productivity Power Tools 动画演示

  4. eclipse 404以及tomcat failed to start错误

    eclipse中的servlet项目有时会不编译,不编译可能就会出现404错误,因为在build path的输出目录并没有class文件,然而如果在输出目录引入之前编译的class文件,就可能出现cl ...

  5. JS定时器的使用--无缝滚动

    <title>无标题文档</title> <style> * {margin:0; padding:0;} #div1{width:1172px; height:2 ...

  6. 2017 ICPC&sol;ACM 沈阳区域赛HDU6223

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  7. php中的public、protected、private三种访问控制模式及self和parent的区别&lpar;转&rpar;

    php的public.protected.private三种访问控制模式的区别 public: 公有类型 在子类中可以通过self::var调用public方法或属性,parent::method调用 ...

  8. ionic3用极光推送笔记

    安卓 环境:ionic3  + 极光 "jpush-phonegap-plugin": "^3.4.3" "cordova-plugin-jcore& ...

  9. centos su命令

    有很多指令都只可以用 root 身份去执行,因此我们需要成为 root 用户.要这样做,我们可以使用 su 指令(更替用户).su 指令有下列格式: su - <user>或su < ...

  10. 【html】前端实现筛选条件跳转

    之前与PHP的合作模式之一是前端这边负责写好静态页面交货. 那现在新进的公司,PHP说筛选由前端来实现. 嗯,好吧.实现就实现,多锻炼下咯. <div class="fliter&qu ...