You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复Python Animal类动物声音功能Bug及相关实现咨询

问题描述

我正在开发一个名为Animal的Python类,该类接收动物类型作为输入并返回对应动物的叫声。但运行代码时遇到以下问题,需协助修复Bug:

  • TypeError:运行时抛出与type属性相关的错误。
  • AttributeError:the_type setter存在异常,无法设置正确的动物叫声。
  • 输出异常:__str__方法无法正确显示动物叫声。

此外,请问如何确保__str__方法能正确输出存储在_the_type中的动物叫声?


原代码

class Animal:
    def __init__(self, type, the_type):
        self.type=type
        self.the_type=the_type
    def __str__(self):
        return f"the sound of the animal {self.type} is \n {self._the_type} "
    @classmethod
    def animal_sound(cls):
        type=input("What is the animal: ").strip().lower()
        
        return cls(type)
    @property
    def the_type(self):
        return type
    @ the_type.setter
    def the_type(self,type):
        match self._the_type:
            case "dog":
                return "wof wof"
            case "cat":
                return "meaw meaw"
            case "horse":
                return "neh neh"
            case _ :
                return "i don't know"
        return type

def animal_sound():
    animal=Animal.animal_sound()
    print(animal)

if __name__ == "__main__":
    animal_sound()

修复后的代码

class Animal:
    def __init__(self, animal_type):
        # 重命名参数避免与内置type关键字冲突,初始化动物类型
        self.type = animal_type
        # 通过属性设置对应叫声
        self.the_type = animal_type

    def __str__(self):
        return f"the sound of the animal {self.type} is:\n{self._the_type}"

    @classmethod
    def animal_sound(cls):
        animal_type = input("What is the animal: ").strip().lower()
        return cls(animal_type)

    @property
    def the_type(self):
        # 返回存储叫声的私有属性
        return self._the_type

    @the_type.setter
    def the_type(self, animal_type):
        # 根据动物类型匹配对应叫声并赋值给私有属性
        match animal_type:
            case "dog":
                self._the_type = "wof wof"
            case "cat":
                self._the_type = "meaw meaw"
            case "horse":
                self._the_type = "neh neh"
            case _:
                self._the_type = "i don't know"

def animal_sound():
    animal = Animal.animal_sound()
    print(animal)

if __name__ == "__main__":
    animal_sound()

修复说明

1. 解决TypeError

  • 原__init__定义了两个参数,但类方法animal_sound实例化时仅传入一个参数,修改__init__仅接收animal_type参数(重命名避免和内置type冲突)。
  • 原@property的the_type错误返回内置函数type,改为返回实例私有属性self._the_type。

2. 解决AttributeError

  • 修正setter装饰器的空格问题:@ the_type.setter改为@the_type.setter。
  • setter方法中,原代码错误匹配未初始化的self._the_type,改为匹配传入的animal_type参数,并将匹配到的叫声赋值给self._the_type(setter的作用是设置属性而非返回值)。

3. 解决输出异常

  • 确保__str__中调用的self._the_type是被setter正确赋值的私有属性,调整字符串格式让输出更清晰。

确保__str__正确输出_the_type的方法

  1. 规范属性赋值逻辑:统一通过the_type属性的setter方法设置_the_type值,避免直接修改私有属性,保证属性值的一致性。
  2. 直接引用私有属性或通过property获取:在__str__中可以直接写self._the_type,或者写self.the_type(调用property的getter方法),两种方式都能拿到正确的叫声值。
  3. 初始化时完成属性赋值:在__init__中通过self.the_type = animal_type触发setter,确保实例创建时_the_type已被正确初始化。

内容的提问来源于stack exchange,提问作者T.A.G.AHUTS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:55:09