热线电话:13121318867

登录
2018-10-23 阅读量: 884
python错误解析(六)

代码如下:

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()

执行上面一段代码,错误如下:

代码如下:

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

【错误分析】Python中调用类的方法,必须与实例绑定,或者调用自身.

代码如下:

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

所以上面代码,要调用Super类的话,只需要加个self参数即可。

代码如下:

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()

#输出结果

>>>   
Sub's method
Super's method
Over...
0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子