Why does the class `__dict__` hold only methods and the instance `__dict__` only data — and why call the getter anyway?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The class SimpleWorkout from the video is already defined for you, with the constructor that expects a start time string, an end time string and the calories burned. Build one workout that starts at '9/30/2021 1:35 PM', ends at '9/30/2021 1:57 PM' and burned 200 calories. Assign the new object to my_workout.
class SimpleWorkout:
'''A simple class to keep track of workouts'''
def __init__(self, start, end, calories):
self.start = start
self.end = end
self.calories = calories
self.icon = 'sweat'
self.kind = 'Workout'
def get_calories(self):
return self.calories
def get_start(self):
return self.start
def get_end(self):
return self.end
def set_calories(self, calories):
self.calories = calories
def set_start(self, start):
self.start = start
def set_end(self, end):
self.end = endCreating an instance, with getters and setters
Write my_workout = SimpleWorkout('9/30/2021 1:35 PM', '9/30/2021 1:57 PM', 200) and the six one-line methods get_calories, get_start, get_end, set_calories, set_start and set_end.
The class state dictionary
Record what print(SimpleWorkout.__dict__.keys()) lists, every method defined plus __module__, __doc__, __dict__ and __weakref__, and what the values behind the docstring key and the method keys hold.
The object state dictionary, and going through the getter
Write the output dict_keys(['start', 'end', 'calories', 'icon', 'kind']) with each value, and state the information-hiding rule that prefers my_workout.get_calories() over reaching for my_workout.calories.