Python:AttributeError:“NoneType”对象没有属性“type”

2024-10-04 03:16:45 发布

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

我创建了一个敌方类,它从文件中读取数据以获取其实例变量。 针对不同类型的敌人,文件被写入不同的数组。他们工作得很好。当我把它们写到对象然后尝试打印它们时,我得到了一个错误:

AttributeError: 'NoneType' object has no attribute 'type'

如果我不打印'type'属性,那么它会显示:

^{pr2}$

课程是:

def Enemy(object):
    def __init__(self, TypeStats):
        self.type = TypeStats[0][0]
        self.health = int(TypeStats[0][1])
        self.strength = int(TypeStats[0][2])
        self.dead = False

将数组写入对象并打印变量的代码是:

Bandit = Enemy(BanditStats)
print Bandit.type, Bandit.health, Bandit.strength 

我将其作为二维数组写入的原因是,当我将数据从文件写入数组时,它会创建一个数组:

BanditStats = [['Bandit', '80','7']]

我不知道为什么会这样,很容易解决吗?在


Tags: 对象selfobjectdeftype数组读取数据strength