python3 super().__init__() 和 __init__() 的区别
1、单继承
super().__int__()和 Base.__init__(self)是一样的, super()避免了基类的显式调用。
class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Create ChildClassA')
super().__init__() ChildClassA()
#输出
#Create ChildClassA
#Create Base
2、多继承
注意:多继承时,会设计继承顺序,supper()相当于返回继承顺序的下一个类,而不是父类。
def GetSupperOrder(class_name,self):
mro = self.__class__.mro()
# #mro()用来获得类的继承顺序。
return mro[mro.index(class_name) + 1] class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Enter ChildClassA')
super().__init__()
print('Leave ChildClassA') class ChildClassB(Base):
def __init__(self):
print('Enter ChildClassB')
super().__init__()
print('Leave ChildClassB') class ChildClassC(ChildClassA,ChildClassB):
pass c = ChildClassC()
print(c.__class__.__mro__)
# 输出:
#Enter ChildClassA
#Enter ChildClassB
#Create Base
#Leave ChildClassB
#Leave ChildClassA
#(<class '__main__.ChildClassC'>, <class '__main__.ChildClassA'>, <class '__main__.ChildClassB'>, <class '__main__.Base'>, <class 'object'>) 从以上结果,可以看出:
super()和父类没有关系,继承执行的顺序是 ChildClassA → ChildClassB → Base → Object 执行过程:
首先初始化ChlidClassC() ,初始化时先调用ChildClassA的构造方法__Init__(),
进而调用super().__init__()方法,此方法返回当前类的继承顺序中ChildClassA后的一个类ChildClassB,然后在执行ChildClassB的构造方法,
最后执行Base的构造方法,然后依次返回,并执行完成。
在多重继承中 ,ChildClassA()中的 super().__init__() 换成Base.__init__(self),在执行时,继承childA后就会直接跳到Base类里,而略过了ChildClassB:
Enter ChildClassA
Create Base
Leave ChildClassA
(<class '__main__.ChildClassC'>, <class '__main__.ChildClassA'>, <class '__main__.ChildClassB'>, <class '__main__.Base'>, <class 'object'>)
从super()方法可以看出,super()的第一个参数可以是继承链中任意一个类的名字,
如果是本身就会依次继承下一个类;
如果是继承链里之前的类便会无限递归下去;
如果是继承链里之后的类便会忽略继承链汇总本身和传入类之间的类;
比如将ChidClassA()中的super改为:super(ChidClassC, self).__init__(),程序就会无限递归下去。
Enter ChildClassA
Enter ChildClassA
Enter ChildClassA
...
Enter ChildClassA
Enter ChildClassA
Enter ChildClassA
Enter ChildClassA
File "D:/Python20190819/WebApp/venv/Include/testunit.py", line 53, in <module>
c = ChildClassC()
File "D:/Python20190819/WebApp/venv/Include/testunit.py", line 41, in __init__
super(ChildClassC, self).__init__()
File "D:/Python20190819/WebApp/venv/Include/testunit.py", line 41, in __init__
super(ChildClassC, self).__init__()
File "D:/Python20190819/WebApp/venv/Include/testunit.py", line 41, in __init__
super(ChildClassC, self).__init__()
[Previous line repeated 992 more times]
File "D:/Python20190819/WebApp/venv/Include/testunit.py", line 40, in __init__
print('Enter ChildClassA')
RecursionError: maximum recursion depth exceeded while calling a Python object
3、super()避免重复调用
如果ChildClassA继承Base, ChildClassB继承ChildClassA和Base,如果ChildClassB需要调用Base的__init__()方法时,就会导致__init__()被执行两次:
"""
单继承 super().__int__()和 Base.__init__(self)是一样的, super()避免了基类的显式调用。 class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Create ChildClassA')
super().__init__() class ChildClassB(Base):
def __init__(self):
print('Create ChildClassB')
super().__init__() ChildClassA()
#输出
#Create ChildClassA
#Create Base
""" """
多继承:
注意:多继承时,会设计继承顺序,supper()相当于返回继承顺序的下一个类,而不是父类。
""" class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Enter ChildClassA')
Base.__init__(self)
print('Leave ChildClassA') class ChildClassB(ChildClassA,Base):
def __init__(self):
print('Enter ChildClassB')
ChildClassA.__init__(self)
Base.__init__(self)
print('Leave ChildClassB') b = ChildClassB() # 输出:
Enter ChildClassB
Enter ChildClassA
Create Base
Leave ChildClassA
Create Base
Leave ChildClassB
supper() 避免重复
"""
单继承 super().__int__()和 Base.__init__(self)是一样的, super()避免了基类的显式调用。 class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Create ChildClassA')
super().__init__() class ChildClassB(Base):
def __init__(self):
print('Create ChildClassB')
super().__init__() ChildClassA()
#输出
#Create ChildClassA
#Create Base
""" """
多继承:
注意:多继承时,会设计继承顺序,supper()相当于返回继承顺序的下一个类,而不是父类。
""" class Base(object):
def __init__(self):
print('Create Base') class ChildClassA(Base):
def __init__(self):
print('Enter ChildClassA')
super( ).__init__()
print('Leave ChildClassA') class ChildClassB(ChildClassA,Base):
def __init__(self):
print('Enter ChildClassB')
super().__init__()
print('Leave ChildClassB') b = ChildClassB() # 输出:
Enter ChildClassB
Enter ChildClassA
Create Base
Leave ChildClassA
Leave ChildClassB
参考自:开源中国 http://my.oschina.net/jhao104/blog/682322
python3 super().__init__() 和 __init__() 的区别的更多相关文章
- python中的__init__和__new__的区别
一.__init__ 方法是什么?(init前后的线是双下划线) 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例 ...
- Python中__init__和__new__的区别详解
__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: # -*- cod ...
- python中的super( test, self).__init__()
python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...
- python3.6 子类的__init__调用父类的__init__
python3.6 子类的__init__调用父类的__init__ 父类 class worker: def __init__(self): self.a=1 self.b=2 if __name_ ...
- super(Student,self).__init__()初始化的是什么东西?
继承不是为了继承里面原来的属性和值么,不初始化的话,会有什么问题? 2015-04-04源自:python进阶 5-17642 浏览2 回答 最佳回答 2015-05-05 1 super(Stude ...
- __init__ 和__new__的区别
__init__和__new__的区别 __init__是当实例对象创建完成后被调用的,然后设置对象属性的一些初始值. __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例, ...
- Python super(Todo,self).__init__() TypeError: super() argument 1 must be type, not classobj
示例如下 class A(): def __init__(self):pass class B(A): def __init__(self): super(A, self).__init__() 当调 ...
- python3 super().__init__()
父类不会自动调用__init__方法 class A: def __init__(self): A = 'A' self.a = 'a' print('init A') class B(A): def ...
- __init__和__new__的区别
根据官方文档: __init__是当实例对象创建完成后被调用的,然后设置对象属性的一些初始值. __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例,是个静态方法. 也 ...
随机推荐
- CSS浮动特性
float:left/right左浮动有浮动 特点: ①浮动不占位:浮动元素不占位置 ②默认排列成一行,遇到边界自动换行 ③如果有文字(没有设置浮动的元素内容)会绕着浮动元素走 <!DOCTYP ...
- 自动化测试 selenium 模块 webdriver使用(一)
一.webdriver基本使用命令 from selenium import webdriver # 导入webdriver模块 >>> chrome_obj = webdriver ...
- NLP传统基础(1)---BM25算法---计算文档和query相关性
一.简介:TF-IDF 的改进算法 https://blog.csdn.net/weixin_41090915/article/details/79053584 bm25 是一种用来评价搜索词和文档之 ...
- MySQL 进阶3 排序查询
#进阶3 排序查询 格式: select 查询列名 from 表 [where 筛选条件] order by 排序列名 [asc / desc] 排序查询/嵌套排序查询/函数查询/[按别名进行 排序] ...
- usb相关
https://github.com/daynix/UsbDk/tree/master/UsbDk 更应该关注下libusb
- linux实操_rpm包和yum包
rpm包的简单查询指令: 查询已安装的rpm列表 rpm -qa | grep xxx 查询火狐浏览器 查询安装的rpm包软件的信息 查询rpm软件包的文件安装在哪里 查询文件属于哪个软件包 卸载rp ...
- 前端知识体系:JavaScript基础-作用域和闭包-JavaScript的作用域和作用域链
JavaScript的作用域和作用域链 作用域: 变量的作用域无非两种:全局作用域和局部作用域 全局作用域: 最外层函数定义的变量拥有全局作用域.即对任何内部函数来说都是可以访问的. <scri ...
- 12、Spring Boot 2.x 集成 MongoDB
1.12 Spring Boot 2.x 集成 MongoDB 完整源码: Spring-Boot-Demos
- paramiko多线程远程执行命令
import paramiko import sys import getpass import threading import os def rcmd(host=None, port=22, us ...
- webuploader+文件夹上传
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...