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

ellie adam
ellie adam
26,376 Points

Animal.noise Q 3 of 3

from animal import Animal class Sheep(Animal): sound = "Meow" pass def animal.noise(): return self.sound.lower() def Sheep.noise():
return Sheep.noise.upper()

sheep.py
from animal import Animal
class Sheep(Animal): 
  sound = "Meow"
  pass
def animal.noise():
   return self.sound.lower()
def Sheep.noise():    
    return Sheep.noise.upper() 

error is task one is not passing any more.

ellie adam
ellie adam
26,376 Points

thanks for helping me out!

2 Answers

Dan Johnson
Dan Johnson
40,532 Points

When you're overriding the methods of a parent class you only need to match the function signature. So in order to override noise you'll use this signature:

def noise(self):
  # Return the uppercased sound string

Don't forget to make sure the method is indented properly. Also you can get rid of pass it's only needed as a placeholder.

ellie adam
ellie adam
26,376 Points

I get this error now.. Bummer! Expected "MEOW", got "meow".

from animal import Animal
class Sheep(Animal): 
  sound = "Meow"
def noise(self):  
    return Sheep.noise.upper() 
Dan Johnson
Dan Johnson
40,532 Points

Your noise method still needs to be indented to be in the class block. Also you'll need to use noise to deal with the string.

ellie adam
ellie adam
26,376 Points

How to deal with string?

Dan Johnson
Dan Johnson
40,532 Points

Refer to sound using self and then call upper on it. Return the result.

ellie adam
ellie adam
26,376 Points

I tried few different things nothing works.

from animal import Animal
class Sheep(Animal): 
  sound = "Meow"
def noise(self):  
    return self.upper() 
def Sheep.noise(self):  
    return self.upper()
def noise(self):  
    return sheep.upper() 

:(

Dan Johnson
Dan Johnson
40,532 Points

self refers to the instance and sound is a property / instance variable / member variable. So to access it you'd do this:

# Accessing the sound property to call upper on it.
self.sound.upper()
ellie adam
ellie adam
26,376 Points
from animal import Animal
class Sheep(Animal): 
  sound = "Meow"
def noise(self):  
  self.sound.upper()

it won't pass.

from animal import Animal
class Sheep(Animal): 
  sound = "Meow"
def noise(self):  
  return self.sound.upper()

it won't let me pass.

Dan Johnson
Dan Johnson
40,532 Points

Careful with the indentation. Also you still need the return:

from animal import Animal

class Sheep(Animal): 
  sound = "Meow"

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