What's wrong with this Python code?


-sharpencoding:utf-8
class Bird:
    def __init__(self):
        self.hungry=True

    def eat(self):
        if self.hungry:
            print (" aaah..")
            self.hungry=False
        else :
            print ("no ,thanks")



class SongBird(Bird):
    def __init(self):
        Bird.__init__(self)
        self.sound = "Squak!"

    def sing(self):
        print(self.sound)


if __name__=="__main__":
    sb=SongBird()
    -sharp["_SongBird__init", "__doc__", "__init__", "__module__", "eat", "hungry", "sing"]
    print (dir(sb))
    sb.sing()
    sb.eat()

error prompt:

AttributeError: SongBird instance has no attribute "sound", prompt has no sound attribute

Feb.28,2021

because it's _ _ init__ instead of _ _ init < del > ha < / del > you're missing two underscores < del > serious < / del >.


use super

Menu