Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在python中向类添加列表?_Python 2.7_Oop - Fatal编程技术网

Python 2.7 如何在python中向类添加列表?

Python 2.7 如何在python中向类添加列表?,python-2.7,oop,Python 2.7,Oop,我正在尝试为python游戏自动生成敌人。这些敌人拥有大量的属性,这些属性会随着发展而变化 我没有以一种有用的方式将这些技能添加到课堂中 combatskills = ["sword", "spear", "axe"] class Enemy(object): def __init__(self): for x in combatskills: self.x = 0 orc1 = Enemy() 目标是最终能够调用orc1.Swar,但我不知

我正在尝试为python游戏自动生成敌人。这些敌人拥有大量的属性,这些属性会随着发展而变化

我没有以一种有用的方式将这些技能添加到课堂中

combatskills = ["sword", "spear", "axe"]

class Enemy(object):
    def __init__(self):
        for x in combatskills:
            self.x = 0

orc1 = Enemy()
目标是最终能够调用orc1.Swar,但我不知道如何动态创建敌人。[战斗技能]属性


有什么想法吗?

原来我在找setattr

class Enemy(object):
    def __init__(self):
        for x in combatskills:
            setattr(self, x, 0)
解决了我想做的事情