When two classes in the same chain both define `get_calories`, how does Python decide which one actually runs?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Workout is already defined above, with its class variable cal_per_hr = 200. Complete RunWorkout so it adds a class variable cals_per_km set to 100, and so its constructor lets Workout set up hours before storing the route on the instance as self.route_gps_points. The checks build RunWorkout(2) and RunWorkout(2, points) and read RunWorkout.cals_per_km, hours and route_gps_points.
class Workout(object):
cal_per_hr = 200
def __init__(self, hours):
self.hours = hoursA GPS route and the cals_per_km class variable
Record the route as a list of (latitude, longitude) tuples, the new class variable cals_per_km = 100 the parent knows nothing about, the parameter route_gps_points=None, and kilometres times calories per kilometre.
A two-branch override falling back to the parent
Write the loop adding gpsDistance(lastP, p) over consecutive points and returning dist * RunWorkout.cals_per_km, the else returning super().get_calories(), and the Boston-to-Newton figures near 1260 and 473.3.
Method lookup up the chain of classes
State the rule: take the type of the object before the dot, use that class's method when it defines one, otherwise climb to the parent and onward to object, then error.