TypeError:对象不可下标

AEU*_*AEU 4 python typeerror

我已收到此错误

  File "/class.py", line 246, in __init__
if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
TypeError: 'Desk' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

我创建了这个对象

class Desk:
     descriptionId = ""
     descriptionStatus = ""
     conceptId = ""
     term = ""
Run Code Online (Sandbox Code Playgroud)

我在另一堂课上叫它

class DescriptionList():

     def deskJ(self,line):
         er = line.strip().split('\t')
         desc = Desk()
         if er[1] == "0":
            desc.descriptionId = er[0]
            desc.descriptionStatus = er[1]
            desc.conceptId = er[2]
            desc.term = er[3]
         return description
Run Code Online (Sandbox Code Playgroud)

然后我在init调用了函数“ deskJ”,并在此部分得到了错误(我删除了该函数的某些部分):

def __init__(self,fitx,konZer,lanZer={}):
        with codecs.open(fitx,encoding='utf-8') as fitx:
            lines = fitx.read().split('\n')[1:-1]
            for line in lines:
                d = self.deskJ(line)
                if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
                    c = konZer.zerrenda[d["conceptId"]]
                    c["fullySpecifiedName"] = d["term"]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

zon*_*ndo 5

使用d["descriptionType"]正在尝试使用d键进行访问"descriptionType"。但是,这d是行不通的,因为这是一个Desk没有键的对象。相反,获取属性:

if d and self.rf == 2 and d.descriptionType in ["900000000000003001"] and d.conceptId in konZer.zerrenda:
Run Code Online (Sandbox Code Playgroud)

  • @Sledge:有一个内置函数:`getattr(d, attr_var)` (3认同)