wangjuju123

2018-10-23   阅读量: 695

数据分析师 Python编程

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...
添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 1 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子