Reputation: 11
I wrote the following code,
#sanitize fuction
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
(mins, secs) = time_string.split(splitter)
return(mins + '.' + secs)
class AthleteList(list):
def __init__(self, a_name, a_dob=None, a_times=[]):
list.__init__([])
self.name = a_name
self.dob = a_dob
self.extend = a_times
def top3(self):
return(sorted(set([sanitize(t) for t in self]))[0:3])
#get coach data fuction
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
templ = data.strip().split(',')
return(AthleteList(templ.pop(0), templ.pop(0), templ))
except IOError as ioerr:
print('File error: ' + str(ioerr))
return(None)
sarah = get_coach_data("sarah2.txt")
julie = get_coach_data("julie2.txt")
james = get_coach_data("james2.txt")
mikey= get_coach_data("mikey2.txt")
vera = AthleteList('vera')
vera.append('1.33')
vera.extend(['1.74','1.46','1.42','1.40'])
print(vera.top3())
When I run this code, it shows the following error. But it only shows error when I use extend method. I can use append method with no probs.
vera.extend(['1.74','1.46','1.42','1.40'])
TypeError: 'list' object is not callable
Upvotes: 0
Views: 399
Reputation: 1
I think the problem is in the definition of class AtheleteList
self.extend = a_times
should be change to self.extend(a_times)
Upvotes: 0
Reputation: 665
To Access list you need to use the square brackets ([]) and not the parenthesis (()).
instead of
vera.extend(['1.74','1.46','1.42','1.40'])
use
aList = ['1.74','1.46','1.42','1.40']
vera.extend(aList)
Upvotes: 1
Reputation: 16
You're basically trying to call a list (self.extend = a_times
and a_times = []
), which is impossible.
What you'd need to do, is use:
vera.extend.extend(['1.74','1.46','1.42','1.40'])
Or either this (as mentioned before):
vera.extend = ['1.74','1.46','1.42','1.40']
But actually this is not exactly correct either (given we need vera
list and not vera.extend
one [missed this when first posted, sorry!]), as shown here:
>>> vera = AthleteList("vera")
>>> vera.extend = ['1.74','1.46','1.42','1.40']
>>> print(vera.top3())
[]
>>> vera.extend.extend(['1.74','1.46','1.42','1.40'])
>>> print(vera.top3())
[]
The correct answer would be remove self.extend from AthleteList class so it works the way you want it (vera.extend(['1.74','1.46','1.42','1.40'])
).
>>> vera = AthleteList("vera")
>>> vera.extend(['1.74','1.46','1.42','1.40'])
>>> print(vera.top3())
['1.40', '1.42', '1.46']
So, class should be like this:
class AthleteList(list):
def __init__(self, a_name, a_dob=None, a_times=[]):
list.__init__([])
self.name = a_name
self.dob = a_dob
def top3(self):
return(sorted(set([sanitize(t) for t in self]))[0:3])
Hope it helps!
Upvotes: 0
Reputation: 1138
According to your code, you have initialized a class variable extend to an empt list.
self.extend = a_times
where a_times = []
It is a variable, not a function according to your code. That is why it is throwing an error of 'not callable'
Change the following line of code:
vera.extend(['1.74','1.46','1.42','1.40'])
to vera.extend = ['1.74','1.46','1.42','1.40']
.
Hope it helps!
Upvotes: 0
Reputation: 24966
Follow the execution path from AthleteList('vera')
, noting how self.extend
gets initialized to a list, which shadows the function you expect.
what's happening is essentially this:
>>> extend = []
>>> extend(['foo'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Upvotes: 1