I am not sure how object of a parent class is created in Python. Consider a following scenario.
class Animal():
def __init__(self):
print("Animal is created")
def eat(self):
print("I am eating")
class Dog(Animal):
def __init__(self, breed, name, spots):
self.breed = breed
self.name = name
self.spots = spots
def bark(self):
print("Woof! My name is {}".format(self.name))
my_dog = Dog(breed="lab", name="Sam", spots=False)
This does not print "Animal is created".
class Animal():
def __init__(self):
print("Animal is created")
def eat(self):
print("I am eating")
class Dog(Animal):
def __init__(self, breed, name, spots):
Animal.__init__(self)
self.breed = breed
self.name = name
self.spots = spots
def bark(self):
print("Woof! My name is {}".format(self.name))
my_dog = Dog(breed="lab", name="Sam", spots=False)
Whereas this prints "Animal is created"
But in both the cases I am able to access eat() method of Animal class from Dogs instance (my_dog). This means Animal is created in both the cases. Then why I don't see Animals constructor getting called in case#1?