0

I'm going through 'Learning Python The Hard Way', and I got to the class lesson. I understood it (or I at least think I did!) and tried to create a simple variation, using my own names, functions, etc...

Now the problem I'm having is that the code won't return anything in command line\powershell. It doesn't have any errors, it just goes to another line of input.

Here's the Code:

class Animal(object):
    '''represents any animal'''
    def __init__(self, legs, size):
        self.legs = legs
        self.size = size

    def detail_animal(self):
        '''show # of legs and size'''
        print "Name: %r\nAge: %r" % (self.legs, self.size)

class canine(Animal):   
    '''represents a canine'''

    def __init__(self, legs, size, hair_length):
        Animal.__init__(self, legs, size)
        self.hair_length = hair_length

    def detail_canine(self):
        Animal.detail(self)
        print 'Has %r inch long hairs.' % self.hair_length

class feral_cat(Animal):
    '''represents a feral cat'''

    def __init__(self, legs, size, tail_length):
        Animal.__init__(self, legs, size)
        self.tail_length = tail_length

    def detail_feral(self):
        Animal.detail(self)
        print "Tail Length: %r" % tail_length

c1 = canine(4, 2, 0.5)
c2 = canine(5, 3, 0.75)
fc1 = feral_cat(4, 5, 3)
a = Animal(4, 2)

Thanks in advance!

4
  • 7
    What do you expect it to do? You never call any of the methods that would print anything, so nothing is ever printed. Commented Jun 6, 2013 at 19:45
  • 2
    to expand on what kindall said, the only functions that print anything are the detail_* functions. You construct a bunch of objects but dont ask for any of the details to be printed. Commented Jun 6, 2013 at 19:48
  • animal_detail also assumes that an animal is named by its number of legs Commented Jun 6, 2013 at 19:50
  • 1
    @JacobAbrahams: The Great Spider of Metebelis Three is named "Eight-Legs", and so are all of her follower-spiders. So, I think we can safely extrapolate from that to all other animate beings. And using size for the age is fine as long as you assume that all animals grow at a linear rate throughout their lifetimes. Commented Jun 6, 2013 at 20:02

1 Answer 1

2

There are a few problems in your code: the Animal class doesn't have a method called detail, which you try to call in all its subclasses. You should probably rename detail_animal(self) to detail(self). To have your program print some output add these lines at the end:

c1.detail_canine()
c2.detail_canine()
fc1.detail_feral()
a.detail()

Furthermore if your program is meant to experiment with method overriding, i.e. the possibility of redefining base class methods in subclasses, you should try and change detail_canine(self) and detail_feral(self) into detail(self). Remember to make the change also in the lines I suggested you should add! You'll see that when you instantiate (i.e. create) an object of the base class Animal's detail(self) method gets called; when you instantiate one of the subclasses that class's detail(self) method gets called instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.