Python中super().__init__()测试以及理解
作者:红鲤鱼与彩虹 时间:2023-08-25 02:22:04
python里的super().init()有什么用?
对于python里的super().__init__()有什么作用,很多同学没有弄清楚。
直白的说super().__init__(),就是继承父类的init方法,同样可以使用super()点 其他方法名,去继承其他方法。
Python super().__init__()测试
测试一、我们尝试下面代码,没有super(A, self).__init__()时调用A的父类Root的属性和方法(方法里不对Root数据进行二次操作)
class Root(object):
def __init__(self):
self.x= '这是属性'
def fun(self):
#print(self.x)
print('这是方法')
class A(Root):
def __init__(self):
print('实例化时执行')
test = A()#实例化类
test.fun()#调用方法
test.x#调用属性
下面是结果:
Traceback (most recent call last):
实例化时执行
这是方法
File "/hom/PycharmProjects/untitled/super.py", line 17, in <module>
test.x # 调用属性
AttributeError: 'A' object has no attribute 'x'
可以看到此时父类的方法继承成功,可以使用,但是父类的属性却未继承,并不能用
测试二、我们尝试下面代码,没有super(A,self).__init__()时调用A的父类Root的属性和方法(方法里对Root数据进行二次操作)
class Root(object):
def __init__(self):
self.x= '这是属性'
def fun(self):
print(self.x)
print('这是方法')
class A(Root):
def __init__(self):
print('实例化时执行')
test = A()#实例化类
test.fun()#调用方法
test.x#调用属性
结果如下
Traceback (most recent call last):
File "/home/PycharmProjects/untitled/super.py", line 16, in <module>
test.fun() # 调用方法
File "/home/PycharmProjects/untitled/super.py", line 6, in fun
print(self.x)
AttributeError: 'A' object has no attribute 'x'
可以看到此时报错和测试一相似,果然,还是不能用父类的属性
测试三、我们尝试下面代码,加入super(A, self).__init__()时调用A的父类Root的属性和方法(方法里对Root数据进行二次操作)
class Root(object):
def __init__(self):
self.x = '这是属性'
def fun(self):
print(self.x)
print('这是方法')
class A(Root):
def __init__(self):
super(A,self).__init__()
print('实例化时执行')
test = A() # 实例化类
test.fun() # 调用方法
test.x # 调用属性
结果输出如下
实例化时执行
这是属性
这是方法
此时A已经成功继承了父类的属性,所以super().__init__()的作用也就显而易见了,就是执行父类的构造函数,使得我们能够调用父类的属性。
上面是单继承情况,我们也会遇到多继承情况,用法类似,但是相比另一种Root.__init__(self),在继承时会跳过重复继承,节省了资源。
还有很多关于super的用法可以参考
super的使用
super() 在 python2、3中的区别
Python3.x 和 Python2.x 的一个区别: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :
python3直接写成 super().方法名(参数)
python2必须写成 super(父类,self).方法名(参数)
例:
python3: super().__init__()
python2: super(父类,self).__init__()
Python3.x 实例:
class A:
def add(self, x):
y = x+1
print(y)
class B(A):
def add(self, x):
super().add(x)
b = B()
b.add(2) # 3
Python2.x 实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class A(object): # Python2.x 记得继承 object
def add(self, x):
y = x+1
print(y)
class B(A):
def add(self, x):
super(B, self).add(x)
b = B()
b.add(2) # 3
来源:https://blog.csdn.net/qq_38787214/article/details/87902291
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL过滤数据操作方法梳理
python datetime处理时间小结
php引用计数器进行垃圾收集机制介绍
详谈构造函数加括号与不加括号的区别
Navicat for MySQL定时备份数据库及数据恢复详解
![](https://img.aspxhome.com/file/2023/4/115554_0s.png)
apache集成php5.6方法分享
![](https://img.aspxhome.com/file/2023/7/55477_0s.png)
python中pass语句用法实例分析
如何用变量实现群聊和悄悄话?
python中子类继承父类的__init__方法实例
JavaScript利用多彩线条摆出心形效果的示例代码
![](https://img.aspxhome.com/file/2023/6/136906_0s.gif)
在 CSS 中关于字体处理效果的思考
![](https://img.aspxhome.com/file/UploadPic/20071/2007131103237363s.gif)
解决TensorFlow GPU版出现OOM错误的问题
解决anaconda安装pytorch报错找不到包的问题
[翻译]标记语言和样式手册 Chapter 8 再谈清单
![](https://img.aspxhome.com/file/UploadPic/20081/29/2008129133019614s.jpg)
基于SQL Server的C/S数据库应用系统
解密CSS Sprites:技巧、工具和教程
![](https://img.aspxhome.com/file/UploadPic/20111/11/pokemon-29s.gif)
python使用正则筛选信用卡
![](https://img.aspxhome.com/file/2023/0/66770_0s.png)
python实现图书馆研习室自动预约功能
![](https://img.aspxhome.com/file/2023/7/83777_0s.png)
ASP开发中可能遇到的错误信息中文说明大全(整理收集)第1/2页
Burpsuite入门及使用详细教程
![](https://img.aspxhome.com/file/2023/9/99389_0s.png)