类型错误:对象不是subscriptab

2024-06-08 01:05:32 发布

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

我得到这个错误

  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

我创建了这个对象

class Desk:
     descriptionId = ""
     descriptionStatus = ""
     conceptId = ""
     term = ""

我在另一个班叫它

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

然后我在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"]

我做错什么了?


Tags: andinselfifinitlinedescclass
1条回答
网友
1楼 · 发布于 2024-06-08 01:05:32

使用d["descriptionType"]试图用键"descriptionType"访问d。但这不起作用,因为d是一个没有键的Desk对象。相反,获取属性:

if d and self.rf == 2 and d.descriptionType in ["900000000000003001"] and d.conceptId in konZer.zerrenda:

相关问题 更多 >

    热门问题