写python class,运行其中的函数,报错
发生异常: TypeError
abinhorse.gen_lists() missing 1 required positional argument: 'lenth'
原代码
class abinhorse:
interval = [1, 1, 2, 3, 5, 8, 15, 30, 60, 120, 240, 365]
out_list = []
def gen_lists(self,lenth):
for i in range(lenth):
temp = i
self.out_list.append(temp)
j = k = self.interval[i]
# for j in range(self.interval[i]):
if j - k == 0:
self.out_list.append(temp)
break;
else:
k -= 1
print(self.out_list)
def main():
ab = abinhorse()
lenth = 3
abinhorse.gen_lists(lenth)
main()
错误分析:
报错说缺少参数length,问题出现在传参的位置。
重新查看函数调用,发现是“类名.函数”的形式调用的,二不是“对象.函数的方式定义的”,与定义的不符。
解决:
把main中的
abinhorse.gen_lists(lenth)
改为
ab.gen_lists(lenth)
问题解决