Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Given these classes, say what each line shows.
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. An object is an instance of its own class and of every class above it. The relation goes one way: up the family tree.
isinstance(s, SportsCar): s was made from SportsCar. True.isinstance(s, Car): a SportsCar is a Car. True.isinstance(s, Vehicle): and so a Vehicle. True.isinstance(Car(), SportsCar): a plain Car is not a sports car. False.issubclass(SportsCar, Vehicle): SportsCar is built on Car, which is built on Vehicle. True.issubclass(Vehicle, Car): the wrong way round. False.Check. Run the program and compare.
Answer: True, True, True, False, True, False.
Log in to join the discussion.
Loading…