Python类没有属性错误。

时间:2022-11-03 14:51:39

This is probably something very obvious but Ive tried everything I can think of and looked here on SO a lot and Im still stuck.

这可能是很明显的事情,但我已经尝试了我能想到的所有东西,并且在这里看了很多,我仍然被困住了。

Ive been working my way through the LearnPythonTheHardWay course and currently on chapter 42 and Ive been playing around with the code there and adding some things:

我一直在学习pythonthehardway的课程,现在在第42章,我一直在摆弄代码并添加一些东西:

http://learnpythonthehardway.org/book/ex42.html

http://learnpythonthehardway.org/book/ex42.html

I added some functions and things to print out for 'Dog' and 'Cat', when I run it with just the print commands for the dog (barks_often and wags_tail) it works fine, when I try it with the print commands for the Cats name is fine but as soon as I add the one for 'drinks_milk' it gives me this error:

我添加了一些功能和打印为“狗”和“猫”,当我运行它的打印命令只有狗(barks_often和wags_tail)效果很好,当我试着它的打印命令猫的名字是好但只要我添加一个“drinks_milk”给了我这个错误:

    PS C:\Users\David\python\lpthw> python ex42_more.py
Rover is a Dog
Rover barks often: True
Rover wags his tail: True
Satan is a Cat
Traceback (most recent call last):
  File "ex42_more.py", line 123, in <module>
    print "%s drinks milk: %s" % (satan.getName(), satan.dri
AttributeError: 'Cat' object has no attribute 'drinksMilk'

What I did to add 'drinks_milk' is exactly the same as what I did for the dog so I dont understand why it doesnt work, I tried renaming it to other things but that didnt work.

我添加“drinks_milk”的方法和我给狗狗做的完全一样,所以我不明白为什么它不奏效,我试着把它重新命名为其他的东西,但那没有用。

This is my code:

这是我的代码:

## Animal is-a object (yes, sort of confusing) look at the extra credit
class Animal(object):
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def getName(self):
        return self.name

    def getSpecies(self):
        return self.species



## class name Dog is-a animal
class Dog(Animal):

    def __init__(self, name, barks_often, wags_tail):
        Animal.__init__(self, name, "Dog")
        self.barks_often = barks_often
        self.wags_tail = wags_tail


    def barksOften(self):
        return self.barks_often

    def wagsTail(self):
        return self.wags_tail




## class Cat is-a animal
class Cat(Animal):

    def __init__(self, name, drinks_milk):
        Animal.__init__(self, name, "Cat")
        self.drinks_milk = drinks_milk


        def drinksMilk(self):
            return self.drinks_milk
## rover is-a Dog
rover = Dog("Rover", True, True)

## satan is-a Cat
satan = Cat("Satan", True)

print "%s is a %s" % (rover.getName(), rover.getSpecies())

print "%s barks often: %s" % (rover.getName(), rover.barksOften())

print "%s wags his tail: %s" % (rover.getName(), rover.wagsTail())

print "%s is a %s" % (satan.getName(), satan.getSpecies())

print "%s drinks milk: %s" % (satan.getName(), satan.drinksMilk())

I have a feeling that this is an indentation problem, you will see that def drinksMilk(self): is indented in a little more than others but this is because when I had it in the normal position and in line with the other defs above and I ran the code it gave me the 'IndentationError: unindent does not match any outer indentation level' error and I couldnt figure out why...

我有一种感觉,这是一个缩进的问题,你会发现def drinksMilk(自我):在比别人更缩进,但这是因为当我在正常位置,符合上述其他def和我跑的代码给了我“IndentationError:unindent不匹配任何外缩进级别的错误,我不能找出原因……

1 个解决方案

#1


2  

Just unindent that method. You can see it working properly here

只是unindent方法。你可以看到它在这里正常工作。

Correct Code:

正确的代码:

class Animal(object):
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def getName(self):
        return self.name

    def getSpecies(self):
        return self.species



## class name Dog is an Animal
class Dog(Animal):

    def __init__(self, name, barks_often, wags_tail):
        Animal.__init__(self, name, "Dog")
        self.barks_often = barks_often
        self.wags_tail = wags_tail


    def barksOften(self):
        return self.barks_often

    def wagsTail(self):
        return self.wags_tail




## class Cat is an Animal
class Cat(Animal):
    def __init__(self, name, drinks_milk):
        Animal.__init__(self, name, "Cat")
        self.drinks_milk = drinks_milk

    def drinksMilk(self):
        return self.drinks_milk

## rover is a Dog
rover = Dog("Rover", True, True)

## satan is a Cat
satan = Cat("Satan", True)

print "%s is a %s" % (rover.getName(), rover.getSpecies())

print "%s barks often: %s" % (rover.getName(), rover.barksOften())

print "%s wags his tail: %s" % (rover.getName(), rover.wagsTail())

print "%s is a %s" % (satan.getName(), satan.getSpecies())

print "%s drinks milk: %s" % (satan.getName(), satan.drinksMilk())

#1


2  

Just unindent that method. You can see it working properly here

只是unindent方法。你可以看到它在这里正常工作。

Correct Code:

正确的代码:

class Animal(object):
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def getName(self):
        return self.name

    def getSpecies(self):
        return self.species



## class name Dog is an Animal
class Dog(Animal):

    def __init__(self, name, barks_often, wags_tail):
        Animal.__init__(self, name, "Dog")
        self.barks_often = barks_often
        self.wags_tail = wags_tail


    def barksOften(self):
        return self.barks_often

    def wagsTail(self):
        return self.wags_tail




## class Cat is an Animal
class Cat(Animal):
    def __init__(self, name, drinks_milk):
        Animal.__init__(self, name, "Cat")
        self.drinks_milk = drinks_milk

    def drinksMilk(self):
        return self.drinks_milk

## rover is a Dog
rover = Dog("Rover", True, True)

## satan is a Cat
satan = Cat("Satan", True)

print "%s is a %s" % (rover.getName(), rover.getSpecies())

print "%s barks often: %s" % (rover.getName(), rover.barksOften())

print "%s wags his tail: %s" % (rover.getName(), rover.wagsTail())

print "%s is a %s" % (satan.getName(), satan.getSpecies())

print "%s drinks milk: %s" % (satan.getName(), satan.drinksMilk())