自定义函数引发异常



所以我最近一直在处理列表,我正试图让一个特殊的类工作,所以我写了一个for循环来测试基本函数,但它只给了我这个回溯:

Traceback (most recent call last):
File "C:UsersUserDesktopnice codev2.py", line 13, in <module>
if Lenlist == len.lenList(len.listLen(List=len.list(list=List().list()))): print(Lenlist);
File "C:UsersUserDesktopnice codev2.py", line 3, in lenList
def lenList(self, list=list): return len(list);
TypeError: 'List' object is not callable

以下是您的来源:

class List:
List = list('list');
def lenList(self, list=list): return len(list);
def listLen(self, List=len): return list(List);
def lenlist(self, len=list): return self.lenList(list=len);
def list(self, list=list): return self.List;
def listlen(self, len=len): return self.list(list=len);
len = List();
Lenlist = 0;
for Len in len.listLen(List=len.list(list=List().list())):
if Len in list(len.listLen(List=len.list(list=Len))):
Lenlist += list(len.listLen(List=len.list(list=Len))).count(Len);
if Lenlist == len.lenList(len.listLen(List=len.list(list=List().list()))): print(Lenlist);

我所期望的是它能打印出这份名单的长度。列表我已经尝试用关键字替换所有的位置参数。

这里有一些关键注意事项需要遵循

  1. 总是尽量避免覆盖python关键字或预定义函数(例如:len(

  2. 类中没有与类名同名的变量名

我看到的第一个问题是,你在lenList中覆盖了len,你赢了;我没有得到你想要的输出我在你的代码中发现的第二个问题是lenList函数试图返回len(list(list是一个python预定义的类,所以len找不到任何

我对你的代码做了一点更改,现在可以打印4个

class List:
List1 = list('list');
def lenList(self, list=list): return len(self.List1);
def listLen(self, List=len): return list(self.List1);
def lenlist(self, len=list): return self.lenList(list=len);
def list(self, list=list): return self.List1;
def listlen(self, len=len): return self.list(list=len);
len1 = List();
Lenlist = 0;
for Len in len1.listLen(List=len1.list(list=List().list())):
if Len in list(len1.listLen(List=len1.list(list=Len))):
Lenlist += list(len1.listLen(List=len1.list(list=Len))).count(Len);
if Lenlist == len1.lenList(len1.listLen(List=len1.list(list=List().list()))): print(Lenlist);

最新更新