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 dof __init_

ID: 3836303 • Letter: C

Question

Consider the following program. class Animal(object): population = 0 dof __init__(solf, 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___()) return "{} says woof!".format (self.name) class Cat (Animal): pass animals = ['Felix' ! ('Cat', None), 'Fido' ! ('Dog', 'mutt'), 'Charlie' ! ('Dog', 'spaniel')} animals_list = [] for k in animals ! if animals [k] [l]; animals_list.append(globals () [animals [k] [0]] (k, animals [k] [1])) else: animals_list.append (globls () [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

(a) output

I am an instance of <class '__main__.Dog'>. My name is Charlie.
My breed is spaniel
Charlie says woof!
I am an instance of <class '__main__.Dog'>. My name is Fido.
My breed is mutt
Fido says woof!
I am an instance of <class '__main__.Cat'>. My name is Felix.
Felix is trying to speak but its method doesn't do much

basicaly this creates three objects (2 dogs and one cat)

b) global() is used to call Dog and Cat class contructor using string name

c) super() is calling parent class methods

d) Cat as it does nothing, its constructor call goes to super and hence Cat object is created.