2020-10-23
阅读量:
914
执行python代码报got nothing instead的错误
问题详述:
代码如下:
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...
59.8855
2
0
关注作者
收藏
评论(0)
发表评论
暂无数据
推荐帖子
0条评论
0条评论
0条评论

