Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Inheritance Override Inherited Methods

Małgorzata Staniszewska
Małgorzata Staniszewska
2,431 Points

how to do this?


from animal import Animal
class Sheep(Animal):

Sheep.sound = "bee"
Sheep.noise() = self.sound.upper()

There are several errors with your code, such that it appears you have a misunderstanding of how classes work. I think you would benefit more from going back and re-watching the video lessons than from me just giving you the answer. Maybe try taking thorough notes on paper, or at least following along in the python interpreter.

Pay careful attention to how attributes and methods are created, when to reference the class instance (the self object), and general indentation/syntax conventions.

Is that kind of mean?

Classes are a really hard concept, even I struggled a lot when I first learned classes.

Respect the level of other people. :)

2 Answers

You're close! There's a few errors, though.

  • You should've had put the code that was after the class inside the class.
  • You can't just make a method with the equal sign, you must use the def keyword.

Try this:

from animal import Animal
class Sheep(Animal):
    sound = "baaaaa"

    def noise():
        return self.sound.upper()

Good luck! ~alex

Note: Don't worry if you make mistakes; classes was really hard for me to learn the first time I learned Python :)

You'll understand it over time :smile:

Małgorzata Staniszewska
Małgorzata Staniszewska
2,431 Points

I've tried like you said but it doesn't work because it doesn't work without "pass" in empty class (task one no longer passing). I can't do this like this. Everything is passing unless I try to put the "noise" method.

What's the task's question?