Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
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. Follow each call, and note which object is self.
| Call | self is | count of that object after |
|---|---|---|
a.add(5) | a | 5 |
b.add(2) | b | 2 |
a.add(1) | a | 6 |
a and b are two different objects, and each keeps its own count. So a.count is 6 and b.count is 2.
Check. Counter.add(a, 5) is the same call as a.add(5): the object before the dot becomes the first argument, self.
Answer: 6 and 2.
Log in to join the discussion.
Loading…