99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
2018-10-23 閱讀量: 923
python錯(cuò)誤解析(六)

代碼如下:

class Super:  
def method(self):
print "Super's method"

class Sub(Super):
def method(self):
print "Sub's method"
Super.method()
print "Over..."

S = Sub()
S.method()

執(zhí)行上面一段代碼,錯(cuò)誤如下:

代碼如下:

>>>   
Sub's method

Traceback (most recent call last):
File "D:\Learn\Python\test.py", line 12, in <module>
S.method()
File "D:\Learn\Python\test.py", line 8, in method
Super.method()
TypeError: unbound method method() must be called with Super instance as first argument (got nothing instead)

【錯(cuò)誤分析】Python中調(diào)用類的方法,必須與實(shí)例綁定,或者調(diào)用自身.

代碼如下:

ClassName.method(x, 'Parm')
ClassName.method(self)

所以上面代碼,要調(diào)用Super類的話,只需要加個(gè)self參數(shù)即可。

代碼如下:

class Super:  
def method(self):
print "Super's method"

class Sub(Super):
def method(self):
print "Sub's method"
Super.method(self)
print "Over..."

S = Sub()
S.method()

#輸出結(jié)果

>>>   
Sub's method
Super's method
Over...
0.0000
1
關(guān)注作者
收藏
評(píng)論(0)

發(fā)表評(píng)論

暫無數(shù)據(jù)
推薦帖子