python提示SongBird instance has no attribute 'sound'

查看:75
本文介绍了python提示SongBird instance has no attribute 'sound'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

提示SongBird instance has no attribute 'sound'

_metaclass_ = type
class Bird:
    def _init_(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Aaaaa.....'
            self.hungry = False
        else:
            print 'No, think'

class SongBird(Bird):
    def _init_(self):
        super(SongBird, self)._init_()
        self.sound = 'Squawk!'
    def sing(self):
        print self.sound

sb = SongBird( )
sb.sing( )

刚开始学python,这是书上的代码。检查过很多遍,就是通不过。

解决方案

_metaclass_ = type
class Bird(object):
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print 'Aaaaa.....'
            self.hungry = False
        else:
            print 'No, think'

class SongBird(Bird):
    def __init__(self):
        super(SongBird, self).__init__()
        self.sound = 'Squawk!'
    def sing(self):
        print self.sound

sb = SongBird( )
sb.sing( )

__init__不是_init_

这篇关于python提示SongBird instance has no attribute 'sound'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆