我正在尝试使用以下代码生成多个类对象:
for playerNum in xrange(players): # Players is usually between 2-8.
vars["player"+str(playerNum)] = Player() # Player() is a class.
我意识到这是一种可怕的方式,所以我能做些什么呢?他们唯一被分配的是Player类。
答案 0 :(得分:0)
只需将它们放入列表或字典中即可。
player_list = [Player() for _ in xrange(players)]
或
player_dict = {'player%s' % i : Player() for i in xrange(players)}