Python中super().__init__()測(cè)試以及理解
python里的super().init()有什么用?
對(duì)于python里的super().__init__()有什么作用,很多同學(xué)沒有弄清楚。
直白的說super().__init__(),就是繼承父類的init方法,同樣可以使用super()點(diǎn) 其他方法名,去繼承其他方法。
Python super().__init__()測(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): 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è)試二、我們嘗試下面代碼,沒有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__()的作用也就顯而易見了,就是執(zhí)行父類的構(gòu)造函數(shù),使得我們能夠調(diào)用父類的屬性。
上面是單繼承情況,我們也會(huì)遇到多繼承情況,用法類似,但是相比另一種Root.__init__(self),在繼承時(shí)會(huì)跳過重復(fù)繼承,節(jié)省了資源。
還有很多關(guān)于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)文章

基于Python socket的端口掃描程序?qū)嵗a

Python numpy.array()生成相同元素?cái)?shù)組的示例

Python實(shí)現(xiàn)根據(jù)Excel生成Model和數(shù)據(jù)導(dǎo)入腳本

Python matplotlib繪圖可視化知識(shí)點(diǎn)整理(小結(jié))