亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中super().__init__()測(cè)試以及理解

 更新時(shí)間:2021年12月06日 09:02:00   作者:紅鯉魚與彩虹  
__init__()一般用來(lái)創(chuàng)建對(duì)象的實(shí)例變量,或一次性操作,super()用于調(diào)用父類的方法,可用來(lái)解決多重繼承問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Python中super().__init__()測(cè)試及理解的相關(guān)資料,需要的朋友可以參考下

python里的super().init()有什么用?

對(duì)于python里的super().__init__()有什么作用,很多同學(xué)沒(méi)有弄清楚。

直白的說(shuō)super().__init__(),就是繼承父類的init方法,同樣可以使用super()點(diǎn) 其他方法名,去繼承其他方法。

Python super().__init__()測(cè)試

?測(cè)試一、我們嘗試下面代碼,沒(méi)有super(A, self).__init__()時(shí)調(diào)用A的父類Root的屬性和方法(方法里不對(duì)Root數(shù)據(jù)進(jìn)行二次操作)

class Root(object):
    def __init__(self):
        self.x= '這是屬性'

    def fun(self):
    	#print(self.x)
        print('這是方法')
        
class A(Root):
    def __init__(self):
        print('實(shí)例化時(shí)執(zhí)行')

test = A()		#實(shí)例化類
test.fun()	#調(diào)用方法
test.x		#調(diào)用屬性

下面是結(jié)果:

Traceback (most recent call last):

實(shí)例化時(shí)執(zhí)行

這是方法

? File "/hom/PycharmProjects/untitled/super.py", line 17, in <module>

? ? test.x? # 調(diào)用屬性

AttributeError: 'A' object has no attribute 'x'

可以看到此時(shí)父類的方法繼承成功,可以使用,但是父類的屬性卻未繼承,并不能用

測(cè)試二、我們嘗試下面代碼,沒(méi)有super(A,self).__init__()時(shí)調(diào)用A的父類Root的屬性和方法(方法里對(duì)Root數(shù)據(jù)進(jìn)行二次操作)

class Root(object):
    def __init__(self):
        self.x= '這是屬性'

    def fun(self):
    	print(self.x)
        print('這是方法')
        
class A(Root):
    def __init__(self):
        print('實(shí)例化時(shí)執(zhí)行')

test = A()		#實(shí)例化類
test.fun()	#調(diào)用方法
test.x		#調(diào)用屬性

結(jié)果如下

Traceback (most recent call last):

? File "/home/PycharmProjects/untitled/super.py", line 16, in <module>

? ? test.fun()? # 調(diào)用方法

? File "/home/PycharmProjects/untitled/super.py", line 6, in fun

? ? print(self.x)

AttributeError: 'A' object has no attribute 'x'

可以看到此時(shí)報(bào)錯(cuò)和測(cè)試一相似,果然,還是不能用父類的屬性

測(cè)試三、我們嘗試下面代碼,加入super(A, self).__init__()時(shí)調(diào)用A的父類Root的屬性和方法(方法里對(duì)Root數(shù)據(jù)進(jìn)行二次操作)

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('實(shí)例化時(shí)執(zhí)行')


test = A()  # 實(shí)例化類
test.fun()  # 調(diào)用方法
test.x  # 調(diào)用屬性

結(jié)果輸出如下

實(shí)例化時(shí)執(zhí)行

這是屬性

這是方法

此時(shí)A已經(jīng)成功繼承了父類的屬性,所以super().__init__()的作用也就顯而易見(jiàn)了,就是執(zhí)行父類的構(gòu)造函數(shù),使得我們能夠調(diào)用父類的屬性。

上面是單繼承情況,我們也會(huì)遇到多繼承情況,用法類似,但是相比另一種Root.__init__(self),在繼承時(shí)會(huì)跳過(guò)重復(fù)繼承,節(jié)省了資源。

還有很多關(guān)于super的用法可以參考

super的使用

super() 在 python2、3中的區(qū)別

Python3.x 和 Python2.x 的一個(gè)區(qū)別: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

python3直接寫成 super().方法名(參數(shù))

python2必須寫成 super(父類,self).方法名(參數(shù))

例:

python3: super().__init__()

python2: super(父類,self).__init__()

Python3.x 實(shí)例:

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 實(shí)例:

#!/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

總結(jié)

到此這篇關(guān)于Python中super().__init__()測(cè)試以及理解的文章就介紹到這了,更多相關(guān)Python?super().__init__()測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論