“str”对象没有“get_type”属性错误

2024-10-04 11:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一个元素附加到一个列表中,但是我收到了这个错误。我检查这两个对象的类型是否相同,以及它们是否具有相同的类型,然后追加它。下一个函数是我收到错误的地方。类包含元素。在

 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

这是课程:

^{pr2}$

这是我查到名单的地方

from LogDatei.LogDatei import *
from Date.Date import *
from Melldung.Melldung import *
from Time.Time import *
class Repo():

    """
    This class contains the most important methonds
    """

    def __init__(self,filename):

        """
        The initialization function
        :param filename: the name of the file you want to take the information
        """
        self.filename=filename

    def get_all(self):

        """
        This function reads all lines from a file
        It grpups in in object Logare that is build from date,type and melldung
        date and melldung are classes also
        :return: a list that contains all information from file
        """

        rez=[]
        f=open(self.filename,'r')

        for line in f:

            tolks=line.split(' ',3)
            date=tolks[0].split('-',2)
            time=tolks[1].split(':',2)
            melldung=tolks[3].split(':')
            final=melldung[1].split('-')
            #date(date[0],date[1],date[2])
            #time()
            rez.append(Logare(date,time,tolks[2],Melldung(melldung[0],final[0],final[1],melldung[2])))

        return rez

在函数get_all()中,我构建了一个列表,然后将其发送给控制程序,在那里我拥有该函数


Tags: infromimportselfdatereturnthattype
1条回答
网友
1楼 · 发布于 2024-10-04 11:23:32

这可以通过使用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实例将类似于:

^{pr2}$

相关问题 更多 >