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...






評(píng)論(0)


暫無數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論