class bird: def _init_(self): self.hungry = True def eat(self): if self.hungry: print 'I am hungry' self.hungry = False else: print 'No,thanks' class Songbird(bird): def _init_(self): super(Songbird, self)._init_() self.sound = 'Squawk!' def sing(self): print self.sound
a = Songbird() a.sing() a.eat() a.eat()
错误信息:
Traceback (most recent call last): File "bird.py", line 21, in <module> a.sing() File "bird.py", line 18, in sing print self.sound AttributeError: Songbird instance has no attribute 'sound'
@9hills 修改后 Traceback (most recent call last): File "bird.py", line 20, in <module> sb = Songbird() File "bird.py", line 15, in __init__ super(Songbird, self).__init__() TypeError: must be type, not classobj