我尝试将一个元素附加到列表但是我收到了这个错误。我检查2个对象的类型是否相同,如果它们的类型相同,那么我追加它。所以下一个功能是我收到错误的地方。该类包含元素。
--aot
这是班级:
def LogsBeiTyp(self,list,type):
"""
Thid funftion return a list that contains all logs group by a type
:param list: a list that contains all object logare
:return: a list modified in such a way thet elements are gropu by type
"""
l=[]
for el in list:
if el.get_type()==type:
l.append(el)
return l
这是我列表的地方
class Logare():
"""
This class contains information about a log
It is known that each log has a date,type,description
"""
def __init__(self,date,time,type,melldung):
"""
The incresemnt function
:param date: a string which is written as "YYYY-MM-DD"
:param type: a string that contains in cappital letters the type of thr log
:param description: a string that contains more details about the log
"""
self.date=date
self.time=time
self.type=type
self.melldung=melldung
def get_date(self):
return self.date
def get_time(self):
return self.time
def get_type(self):
return self.type
def get_melldung(self):
return self.melldung
def __str__(self):
return '%s %s %s %s'%(self.date,self.time,self.type,self.melldung)
在函数get_all()中,我构建了列表,而不是将其发送到我具有该功能的控制器
答案 0 :(得分:0)
这可以通过使用isinstance来解决。使用这个构造你 可能有:
def LogsBeiTyp(self,list,class_type):
"""
Thid funftion return a list that contains all logs group by a type
:param list: a list that contains all object logare
:return: a list modified in such a way thet elements are gropu by type
"""
l=[]
for el in list:
if isinstance(el, class_type):
l.append(el)
return l
如上所述,使用内置(即类型)作为变量名称的评论并不是一个好主意。
在Logare类中调用它并在元素列表中查找Logare实例将类似于:
my_logs = self.LogsBeiTyp(elements, Logare)