我有以下课程:
class Player(object):
def __init__(self, name, age):
self.name = name
self.age = age
我有一个class(Lobby)
包含玩家的。该类应该为每个玩家提供额外的属性。例如,idle
时间:
class Lobby(object):
def __init__(self, players):
self.players = players
for player in players:
player.idle = 0
而且我还有另一个class(Game)
这个类应该为每个玩家提供额外的属性。例如,team
属性
class Game(object):
def __init__(self, players):
self.players = players
self.organize_teams();
def organize_teams(self):
for player in self.players:
if condition1:
player.team = 0
elif condition2:
player.team = 1
elif condition3:
player.team = 2
问题是玩家的额外属性只与特定的类有关。所以我想了另一种方法来做到这一点。
例如,大厅类:
class Lobby(object):
def __init__(self, players):
self.players = {}
for player in players:
self.players[player] = {idle: 0}
这是一个好习惯吗?