Python中super().__init__()測試以及理解
python里的super().init()有什么用?
對于python里的super().__init__()有什么作用,很多同學(xué)沒有弄清楚。
直白的說super().__init__(),就是繼承父類的init方法,同樣可以使用super()點 其他方法名,去繼承其他方法。
Python super().__init__()測試
?測試一、我們嘗試下面代碼,沒有super(A, self).__init__()時調(diào)用A的父類Root的屬性和方法(方法里不對Root數(shù)據(jù)進行二次操作)
class Root(object): def __init__(self): self.x= '這是屬性' def fun(self): #print(self.x) print('這是方法') class A(Root): def __init__(self): print('實例化時執(zhí)行') test = A() #實例化類 test.fun() #調(diào)用方法 test.x #調(diào)用屬性
下面是結(jié)果:
Traceback (most recent call last):
實例化時執(zhí)行
這是方法
? File "/hom/PycharmProjects/untitled/super.py", line 17, in <module>
? ? test.x? # 調(diào)用屬性
AttributeError: 'A' object has no attribute 'x'
可以看到此時父類的方法繼承成功,可以使用,但是父類的屬性卻未繼承,并不能用
測試二、我們嘗試下面代碼,沒有super(A,self).__init__()時調(diào)用A的父類Root的屬性和方法(方法里對Root數(shù)據(jù)進行二次操作)
class Root(object): def __init__(self): self.x= '這是屬性' def fun(self): print(self.x) print('這是方法') class A(Root): def __init__(self): print('實例化時執(zhí)行') test = A() #實例化類 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'
可以看到此時報錯和測試一相似,果然,還是不能用父類的屬性
測試三、我們嘗試下面代碼,加入super(A, self).__init__()時調(diào)用A的父類Root的屬性和方法(方法里對Root數(shù)據(jù)進行二次操作)
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('實例化時執(zhí)行') test = A() # 實例化類 test.fun() # 調(diào)用方法 test.x # 調(diào)用屬性
結(jié)果輸出如下
實例化時執(zhí)行
這是屬性
這是方法
此時A已經(jīng)成功繼承了父類的屬性,所以super().__init__()的作用也就顯而易見了,就是執(zhí)行父類的構(gòu)造函數(shù),使得我們能夠調(diào)用父類的屬性。
上面是單繼承情況,我們也會遇到多繼承情況,用法類似,但是相比另一種Root.__init__(self),在繼承時會跳過重復(fù)繼承,節(jié)省了資源。
還有很多關(guān)于super的用法可以參考
super() 在 python2、3中的區(qū)別
Python3.x 和 Python2.x 的一個區(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 實例:
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
總結(jié)
到此這篇關(guān)于Python中super().__init__()測試以及理解的文章就介紹到這了,更多相關(guān)Python?super().__init__()測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何獲取響應(yīng)體response body
在Python中,我們可以使用多個庫來發(fā)送HTTP請求并獲取響應(yīng)體(response body),其中requests就是最常用的庫之一,下面我們就來看看如何利用requests發(fā)送HTTP GET請求,并獲取響應(yīng)體吧2024-11-11Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換
這篇文章主要介紹了Python操作JSON實現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級的數(shù)據(jù)交換格式,關(guān)于JSON的更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下2022-06-06Python生成驗證碼、計算具體日期是一年中的第幾天實例代碼詳解
這篇文章主要介紹了Python生成驗證碼、計算具體日期是一年中的第幾天,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10Python實現(xiàn)Tab自動補全和歷史命令管理的方法
這篇文章主要介紹了Python實現(xiàn)Tab自動補全和歷史命令管理的方法,實例分析了tab自動補全的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03