热线电话:13121318867

登录
2018-10-24 阅读量: 801
接上文:python 错误解析(二十三)

代码如下:

class Bird:  
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print "Ahaha..."
self.hungry = False
else:
print "No, Thanks!"

class SingBird(Bird):
def __init__(self):
super(SingBird,self).__init__()
self.sound = 'squawk'
def sing(self):
print self.sound
>>> sb = SingBird()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
sb = SingBird()
File "D:\Learn\Python\Person.py", line 51, in __init__
super(SingBird,self).__init__()
TypeError: must be type, not classobj

【错误分析】在模块首行里面加上__metaclass__=type,具体还没搞清楚为什么要加

代码如下:

__metaclass__=type  
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print "Ahaha..."
self.hungry = False
else:
print "No, Thanks!"

class SingBird(Bird):
def __init__(self):
super(SingBird,self).__init__()
self.sound = 'squawk'
def sing(self):
print self.sound
>>> S = SingBird()
>>> S.
SyntaxError: invalid syntax
>>> S.
SyntaxError: invalid syntax
>>> S.eat()
Ahaha...
0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子