Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
What does this program show? Say it before you run it.
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, compare your working with the solution.
Plan. colour is a class attribute. A write through an object makes an object attribute.
First line:
a.colour = "red" makes a new attribute on a. It does not change the class.a.colour is red. b.colour finds nothing on b, so it uses the class value, white. Light.colour is white.Second line:
Light.colour = "blue" changes the class attribute.a.colour is still red, because a has its own attribute that hides the class one.b.colour is blue, because b uses the class value, which changed.Light.colour is blue.Answer: red white white and then red blue blue.
Log in to join the discussion.
Loading…