Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program. class Animal (object): population = 0 def_init_(

ID: 3837492 • Letter: C

Question

Consider the following program. class Animal (object): population = 0 def_init_(self, name): self. name = name def__str___(self): return "I am an instance of { }. My name is { }. ".format (self. ___class ___, self. name) def___repr_ (self): return self._str __ () def make_sound (self): return *() is trying to speak but its method doesn't do much".format (self. name) class Dog(Animal): def __ init __ (self, name, bread): super (). init __ (name) self. bread = bread def__str ___ (self): print (super (). ___ str __ () return "My bread is { }" .format (self. bread) def make_sound (self): return "() says woof !". format (self. name) class Cat (Animal): pass animals = {'Felix': ('Cat', None), 'Fido': ('Dog', 'mutt'), 'Charlie': ('Dog', 'spaniel')) animals_l1st = [] for k in animals: if animals [k] [1]: animals_list.append (globals()(animals [k] [0]] (k, animals [k] [1])) else: animals_list .appond(globals () [animals [k] [0]] (k)) Animal.population+ = 1 for animal in animals_list: print (animal) print (animal. make_sound ()) print ("Animal population is { }".format(Animal.population)) (a) What output will the program produce? Explain the result. (b) What is the significance of the use of globals () in the program? c) What is the significance of the use of super () in the program? (d) The Cat class appears to do nothing. Explain how a Cat instance would be created.

Explanation / Answer

Commenting the program for better understanding

class Animal(object):
population = 0
def __init__(self, name):
self.name = name

def __str__(self):
return "I am an instance of {}. My name is {}. ".format(self.__class__, self.name)

def __repr__(self):
return self.__str__()
  
def make_sound(self):
return "{} is trying to speak but its method doesn't do much".format(self.name)

class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed

def __str__(self):
print(super().__str__()) //super() used to get super function of __str__ in Animal Class
return "My breed is {}. ".format(self.breed)
  
def make_sound(self):
return "{} says wolf!".format(self.name)

class Cat(Animal):
pass //executes __str__() of Dog class.

animals = {'Felix' : ('Cat', None), 'Fido' : ('Dog' , 'mutt'), 'Charlie' : ('Dog' , 'Spaniel')}
animals_list = []
for k in animals:
#print(animals[k][1])   //animals[k][1] holds values of mutt, None and spaniel
if animals[k][1]: //true for mutt and Spaniel and false for None
animals_list.append(globals()[animals[k][0]](k,animals[k][1])) // executes all classes of Animal,Dog and Cat
else:
animals_list.append(globals()[animals[k][0]](k))
Animal.population+=1 // counts the number of list

for animal in animals_list:
print(animal)   
print(animal.make_sound)

print("Animal population is {}".format(Animal.population))

(a) Output :

I am an instance of <class '__main__.Dog'>. My name is Fido.
My breed is mutt. //with the help of globals(), Animal and Dog class executes __str__()
I am an instance of <class '__main__.Dog'>. My name is Fido.
<bound method Dog.make_sound of My breed is mutt. > //with the help of super(), the Animal Class __str__ works
I am an instance of <class '__main__.Dog'>. My name is Charlie.
My breed is Spaniel.
I am an instance of <class '__main__.Dog'>. My name is Charlie.
<bound method Dog.make_sound of My breed is Spaniel. >
I am an instance of <class '__main__.Cat'>. My name is Felix.
<bound method Cat.make_sound of I am an instance of <class '__main__.Cat'>. My name is Felix. > // with the help of pass in Cat class, __str__() of Dog class executes and calls Animal class with super()
Animal population is 3

(b)

//animals_list.append(globals()[animals[k][0]](k,animals[k][1]))

globals() is used to call the class using class string name and parameters, that is how Animal and Dog class executes.

(c). super() is used to call the base class from derived class. if we call super() from Dog class, then Animal class gets executed.

(d).

Cat class instance

class Cat(Animal):

def __init__(self,name): // init is special method or class constructor.. Initialisation of variables happens

self.name = name