我正在尝试使用循环自动创建一个类的多个对象。
我尝试将每个对象追加到列表中。
fList = []
from fight import fight #the actual battle
class Fighter:
def __init__(self, name, hp, speed, style):
self.name = name
self.hp = hp
self.speed = speed
self.style = style
def roll_call(self):
print('\n')
print('My name is ' + self.name +'.')
print('I have ' + str(self.hp) + ' Hit Points')
print('My speed is ' + str(self.speed) + '.')
print('I fight in the ' + self.style + ' style.')
def create_fighters(number):
for i in range(number):
fList.append(Fighter('Bob'+str(i), 1,1,'dog'))
我希望能够使用setattr()操作每个对象,但是为此必须为其分配一个变量。用列表索引号操作它们似乎很笨拙。