Back to questions
JuniorTheoryCommon
What does the program print?
class Animal:
def __init__(self, name):
self.name, self.sound = name, "hum"
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
self.sound = "woof"
print(Dog("Rex").name, Dog("Rex").sound)Rex woof
- Expecting
Rex hum, as if the parent's value won:super().__init__(name)runs first and setshum, then the next line ofDog.__init__replaces it withwoof. - Expecting an
AttributeErrorforname, as ifDoglost the parent's setup:super().__init__(name)runsAnimal.__init__, which setsself.name.
- What would
Dog("Rex").namegive if thesuper().__init__(name)line were removed? - What would
.soundbe ifDog.__init__set it before callingsuper().__init__(name)?