生成多个重复对象

时间:2014-07-06 08:06:49

标签: python class object

我正在尝试使用以下代码生成多个类对象:

for playerNum in xrange(players):             # Players is usually between 2-8.
    vars["player"+str(playerNum)] = Player()  # Player() is a class.

我意识到这是一种可怕的方式,所以我能做些什么呢?他们唯一被分配的是Player类。

1 个答案:

答案 0 :(得分:0)

只需将它们放入列表或字典中即可。

player_list = [Player() for _ in xrange(players)]

player_dict = {'player%s' % i : Player() for i in xrange(players)}