Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A Dog object has no name. Find the reason and fix the class.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The child wrote its own __init__. That replaces the parent's, so self.name is never set. The line rex.name gives AttributeError: 'Dog' object has no attribute 'name'.
The fix. Let Dog.__init__ take the name as well, and call the parent's set-up with super().
Check. It shows Rex ['sit'].
Answer: Call super().__init__(name) in the child, and pass the name in.
Log in to join the discussion.
Loading…