Page 244 -
P. 244
new athletelist
Here is the code for the now defunct Athlete class. In the space provided below, you were
to rewrite this class to inherit from the built-in list class. You were to call your new class
AthleteList, as well as provide a few lines of code to exercise your new class:
class Athlete:
def __init__(self, a_name, a_dob=None, a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return(sorted(set([sanitize(t) for t in self.times]))[0:3])
def add_time(self, time_value):
self.times.append(time_value) These
methods
aren’t needed
def add_times(self, list_of_times):
The class name self.times.extend(list_of_times) anymore.
has changed. Inherit from the built-in list class.
class AthleteList(list):
def __init__(self, a_name, a_dob=None, a_times=[]):
Nothing new here… list.__init__([])
this code is very self.name = a_name The data
similar to the itself is the
“NamedList” init code. self.dob = a_dob timing data,
so the “times”
self.extend(a_times) attribute is
def top3(self): gone.
return(sorted(set([sanitize(t) for t in self]))[0:3])
Use the new
class’s name.
vera = AthleteList(‘Vera Vi’)
Now that you’re vera.append(‘1.31’) This code does a good job of
inheriting from print(vera.top3()) exercising your new class.
the built-in list,
you can use its vera.extend([‘2.22’, “1-21”, ‘2:22’])
methods to get
your work on. print(vera.top3())
208 Chapter 6