日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonsuper().__init__()

對(duì)于python里的super().__init__()有什么作用,相信有很多同學(xué)沒有弄清楚。直白的說(shuō)super().__init__(),就是繼承父類的init方法,同樣可以使用super()去繼承的方法。下面通過不同的繼承并調(diào)用,向大家介紹super().__init__()的具體使用方法。

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、甕安ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的甕安網(wǎng)站制作公司

子類構(gòu)造函數(shù)調(diào)用super().
子類構(gòu)造函數(shù)調(diào)用super().init()的時(shí)候,會(huì)從父類繼承屬性。

三種構(gòu)造函數(shù)的區(qū)別:

當(dāng)子類不做初始化的時(shí)候,會(huì)自動(dòng)繼承父類的屬性;
當(dāng)子類做初始化(子類中包含新的屬性)的時(shí)候,子類不會(huì)自動(dòng)繼承父類的屬性;
當(dāng)子類做初始化(子類中包含新的屬性)的時(shí)候,如果子類調(diào)用super初始化了父類的構(gòu)造函數(shù),那么子類會(huì)繼承父類的屬性。

class father:
    def __init__(self, father_attribute="father"):
        self.father_attribute=father_attribute

class child_without_ini(father):
    pass

class child_with_ini(father):
    def __init__(self, child_attribute):
        self.child_attribute=child_attribute

class child_with_super(father):
    def __init__(self,father_attribute,super_attribute):
        self.super_attribute=super_attribute
        super().__init__(father_attribute)

test_class_without_ini=child_without_ini()
test_class_with_ini=child_with_ini('child')
test_class_with_super=child_with_super('new_father','super')


測(cè)試:

print(test_class_without_ini.father_attribute)
輸出:
father

print(test_class_with_ini.father_attribute)
輸出:
AttributeError: 'child_with_ini' object has no attribute 'father_attribute'

print(test_class_with_super.father_attribute)
輸出:
new_father

另一種使用方法:
super(class,self).init()

其中class是子類,這段代碼的含義是首先找到class的父類,然后將class類的對(duì)象轉(zhuǎn)化為父類的對(duì)象,讓后讓這個(gè)“被轉(zhuǎn)化”的對(duì)象調(diào)用自己的__init__()函數(shù)。

class child_with_super(father):
    def __init__(self,father_attribute,super_attribute):
        super(child_with_super, self).__init__(father_attribute)
        self.super_attribute=super_attribute

文章來(lái)源于網(wǎng)絡(luò),如有雷同,請(qǐng)聯(lián)系作者。


網(wǎng)頁(yè)題目:創(chuàng)新互聯(lián)Python教程:pythonsuper().__init__()
當(dāng)前URL:http://www.5511xx.com/article/ccdiccj.html