繁体   English   中英

Python:AttributeError:'NoneType'对象没有属性'type'

[英]Python: AttributeError: 'NoneType' object has no attribute 'type'

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

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

如果我不打印'type'属性,则会显示:

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

该类是:

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 

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

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

我不知道为什么会这样容易解决?

您不使用def定义类。 应该是class

class Enemy:
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM