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

Please help Why does it still get lower case when I have infused upper case all over?

Challenge Task 3 of 3

Animal.noise() returns self.sound.lower(). Make Sheep.noise() return the uppercased version of the instance's sound.

Why does it return expected "SNEEZE" but got "sneeze"? Surely I have put lots of upper() all over. Is there a bug in this task?

sheep.py
from animal import Animal
class Sheep(Animal):

    sound = "SNEEZE".upper()
def __init__(self, sound):
    self.sound = 'SNEEZE'.upper
    return self.sound.upper()

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

The key here is inheritance. The challenge checker is running Sheep.noise() to get the sound. If there is not a method name noise() in Sheep, it will try to run the inherited method from the parent class. This is Animal.noise() which returns self.sound.lower().

To solve the challenge, create a Sheep method noise which will override the inherited Animal.noise(). It is in the Sheep.noise() that you use the upper() method to get the result you are looking for.

Post back if you have more questions. Good Luck!!!

I tried all sort of things including this:

from animal import Animal
class Sheep(Animal):
    pass
    sound = "SNEEZE".upper()
def __init__(Sheep, noise):
    Sheep.noise = 'SNEEZE'.upper
    return Sheep.noise.upper()

It still fails

Finally:

from animal import Animal

class Sheep(Animal):
    sound = 'roar'

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

It is not making sense at all, but it worked!!