2 sinat 28458775 sinat_28458775 于 2015.05.29 22:39 提问

继承python内置的list,在创建实例时,提示参数个数不正确

def sanitize(time_string):
if "-" in time_string:
splitter = "_"
elif ":" in time_string:
splitter = ":"
else:
return time_string
(mins, secs) = time_string.strip().split(splitter)
return mins, ".", secs

class AthleteList(list):
def int(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]

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")
print sarah.name, "'s fastest times are: ", str(sarah.top3())

错误提示:
Traceback (most recent call last):
File "C:\Python27\listclass2.py", line 30, in
sarah = get_coach_data("sarah2.txt")
File "C:\Python27\listclass2.py", line 25, in get_coach_data
return AthleteList(templ.pop(0), templ.pop(0), templ)
TypeError: list() takes at most 1 argument (3 given)

不知道怎么改。。

3个回答

oyljerry
oyljerry   Ds   Rxr 2015.05.29 23:06

提示没有这个属性name。

sinat_28458775
sinat_28458775 不好意思,刚刚的错误提示是试着更改后的,现在是当前代码的错误提示,麻烦再看一下,谢谢
2 年多之前 回复
devmiao
devmiao   Ds   Rxr 2015.05.29 23:08

看下构造函数参数个数是否匹配。

open_wise
open_wise   2015.05.30 10:05

把参数写成*argv,你试试看。

Csdn user default icon
上传中...
上传图片
插入图片