Why does `Person` call `Animal.__init__(self, age)` by class name instead of just repeating the two lines the parent already wrote?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Cat was happy with the constructor it inherited, because a cat is built from an age alone. A person should be built from a name and an age, and Animal's constructor has no parameter for a name — so Person has to define its own.
Complete the class below so that building a person sets up the age through Animal's own constructor, called the long way on the class name rather than assigning self.age yourself; puts the name in place with the inherited setter set_name; and starts the person off with an empty list stored in self.friends. Then build the person "jack", age 30, and assign him to person.
class Animal(object):
def __init__(self, age):
self.age = age
self.name = None
def get_age(self):
return self.age
def get_name(self):
return self.name
def set_age(self, newage):
self.age = newage
def set_name(self, newname=""):
self.name = newname
def __str__(self):
return "animal:" + str(self.name) + ":" + str(self.age)Calling the parent initializer by class name
Write Person.__init__(self, name, age) containing Animal.__init__(self, age) with every parameter including self, then self.set_name(name) and self.friends = [], and record the two data attributes that one parent call creates.
What a person adds to an animal
List get_friends returning a copy of the list, add_friend refusing duplicates, speak printing hello, age_diff printing the absolute gap, and __str__ giving person:name:age, with get_name and get_age inherited.
make_pets over a dict of Person to Cat
Write the for k,v in d.items() loop with the note that k is a Person and v a Cat, calling k.get_name() and v.get_name() to print ana:furball.