Python继承返回属性错误

时间:2015-10-14 16:54:10

标签: python inheritance

刚刚开始使用Python,我是Derek Banas的粉丝并且一直在关注一个教程,而且我仍然坚持使用一些代码。

class Dog(Animal):
    __owner = ""

    def __init__(self, name, height, weight, sound, owner):
        self.__owner = owner
        super(Dog, self).__init__(name, height, weight, sound)

    def set_owner(self, owner):
        self.__owner = owner

    def get_owner(self):
        return self.__owner

    def get_type(self):
        print("Dog")

    def tostring(self):
        return "{} is {} cm tall and {} kilograms and say {} His owner is {}".format(self.__name,
                                                                                     self.__height,
                                                                                     self.__weight,
                                                                                     self.__sound,
                                                                                     self.__owner)

    def multiple_sounds(self, how_many=None):
        if how_many is None:
            print(self.get_sound())
        else:
            print(self.get_sound() * how_many)

spot = Dog("Spot", 53, 27, "Ruff", "Seb")

print(spot.tostring())

我收到以下错误:

Traceback (most recent call last):
  File "G:/JetBrains/PyCharm Community Edition 4.5.4/PyCharm Projects/Testing 123/testing objects.py", line 87, in <module>
    print(spot.tostring())
  File "G:/JetBrains/PyCharm Community Edition 4.5.4/PyCharm Projects/Testing 123/testing objects.py", line 73, in tostring
    return "{} is {} cm tall and {} kilograms and say {} His owner is {}".format(self.__name,
AttributeError: 'Dog' object has no attribute '_Dog__name'

正如我之前从使用vb编程转移的那样,'tostring'方法格式的缩进让我感到困惑。我甚至尝试将它全部放在一行中,它仍然无法识别Animal类中的继承属性'__name'。

帮助将不胜感激。

编辑:

值得一提的是,我正在使用Pycharm来编写所有这些内容。

这里也是动物类

class Animal:
    __name = ""
    __height = 0
    __weight = 0
    __sound = 0

    def __init__(self, name, height, weight, sound):
        self.__name = name
        self.__height = height
        self.__weight = weight
        self.__sound = sound

    def set_name(self, name):
        self.__name = name

    def set_height(self, height):
        self.__height = height

    def set_weight(self, weight):
        self.__weight = weight

    def set_sound(self, sound):
        self.__sound = sound

    def get_name(self):
        return self.__name

    def get_height(self):
        return self.__height

    def get_weight(self):
        return self.__weight

    def get_sound(self):
        return self.__sound

    def get_type(self):
        print("Animal")

    def tostring(self):
        return "{} is {} cm tall and {} kilograms and say {}".format(self.__name,
                                                                     self.__height,
                                                                     self.__weight,
                                                                     self.__sound)

3 个答案:

答案 0 :(得分:4)

您的Animal班级正在使用Name Mangling。来自documentation -

  

由于类私有成员有一个有效的用例(即为了避免名称与子类定义的名称冲突),对这种机制的支持有限,称为 name mangling 表单__spam 的任何标识符(至少两个前导下划线,最多一个尾随下划线)在文本上替换为_classname__spam ,其中classname为剥离前导下划线的当前类名。

(强调我的)

因此,在您的Animal课程定义后,任何名称(例如__name)都会更改为_Animal__name等等。您还需要在{{1}中访问它们}。class。

但我认为你实际上不需要使用 Name Mangling ,如果你不是指 Name Mangling ,你应该避免使用两个前导下划线发生。

答案 1 :(得分:2)

更改您的功能代码

来自

def toString(self):
        return "{} is {} cm tall and {} kilograms and say {} and the owner is {}".format(
        self.__name,
        self.__height,
        self.__weight,
        self.__sound,
        self.__owner)

def toString(self):
        return "{} is {} cm tall and {} kilograms and say {} and the owner is {}".format(
        self.get_name(),
        self.get_height(),
        self.get_weight(),
        self.get_sound(),
        self.get_owner())

答案 2 :(得分:0)

子类本身没有属性,请改用它。

return super(Dog, self).toString() + "His owner is {}".format(self.__owner)