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 print
s 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.