2

I have been learning Python and I finally understood what inheritance means between classes and objects. So here is my code I want to make sure I got this down right:

class Animal(object):
    def __init__(self, name):
        self.name = name
        print self.name
    def howl(self, name):
        print "Eeh %s" % name


class Dog(Animal):
    def __init__(self, name):
        ##has-a __init__ function that takes self and name parameters.
        self.name = name
        print "%s barks and is happy" % self.name
    def sound(self, name):
        print "%s barks" % name


rover = Dog("Rover")

rover.sound("Rover")

rover.howl("Rover")

In order to better understand the way my classes behaved with the "base class" Animal, I put prints all over the place and I can see that Dog is able to call howl, a function from its parent class, Animal (Is that right?)

My other question was that when I use rover = Dog("Rover"), how come it's using __init__ function call? What is the purpose of an __init__ function when all it really does is set a value to a variable (self.name)? Because nobody calls rover.__init__("Rover"), and you can't do a print(rover = Dog("Rover")) and why didn't the __init__ function of the Animal class print out?

Just asking for clarification here on class inheritance and function behaviors between related classes.

El Bert
  • 2,958
  • 1
  • 28
  • 36
OrangeLime
  • 105
  • 3
  • 10

2 Answers2

2

if you construct a class, you will need init funtion. Because your override the __init__ function, if you don't write __init__ function in Dog, it will use Animal's init function. If you want to use Animal's init function, you can do just like this:

class Dog(Animal):
    def sound(self, name):
        print "%s barks" % name
edwardramsey
  • 363
  • 2
  • 12
2

Your post contains multiple questions so here we go!

1 Dog is able to call howl a function from its parent class, Animal (Is that right?)

Yes you are right. Since you declared you Dog class as inheriting from Animal - class Dog(Animal) - the Dog class will have all attributes - including methods - declared in Animal hence you can call howl on a dog.

2 when I use rover = Dog("Rover"), how come it's using __init__ function call?

When creating an object using ClassName(<parameters>) what is happening behind the scene, among other things, is that Python will call the __init__ method defined in the class ClassName. For more details see the __init__ documentation, which also indicates why a class without an __init__ method will call its parent's __init__ method. This documentation also answer your sub-question What is the purpose of an __init__ function ... ?. The overall idea is that you (almost) never call the __init__ method directly.

Now for the fun part!

3 why didn't the __init__ function of the Animal class print out?

In your code example the __init__ method of Animal and Dog are almost similar i.e. set the name attribute and print something. The only difference being what they print. So let's imagine you rewrite your classes like this:

class Animal(object):

    def __init__(self, name):
        self.name = name
        print "I am an Animal named %s" % self.name

class Dog(Animal):

    def __init__(self, name):
        super(Dog, self).__init__(name) # That's the important line
        print "I am actually a Dog named %s, I bark too!" % self.name

rover = Dog("Rover")
# Output:
# I am an Animal named Rover
# I am actually a Dog named Rover, I bark too!

As the commend explained the import line is when we call super(Dog, self).__init__(name) - see documentation - it will retrieve the super class of Dog and then we call the __init__ method to initialize the current object self - hence setting name and calling the print statement in the Animal class.

With all this you end up with the correct calls to print!

Note that you have several ways to call the super class's __init__:

Animal.__init__(self, name)
super(Dog, self).__init__(name)
super(self.__class__, self).__init__(name) # self.__class__ is the class of self or its type.

See this question for a more in depth comparison of the various syntaxes.

Community
  • 1
  • 1
El Bert
  • 2,958
  • 1
  • 28
  • 36