欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python中super().__init__()测试以及理解

时间:2022-08-08 08:21:03|栏目:Python代码|点击:

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

总结

上一篇:Python数据结构之队列详解

栏    目:Python代码

下一篇:Python中numpy数组的计算与转置详解

本文标题:Python中super().__init__()测试以及理解

本文地址:http://www.codeinn.net/misctech/210199.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有