我有一个问题,因为我自己无法解决。
我有,让我们说玩家类。 (多人游戏)
class Player(object):
def __init__(self, account_name, hide_name_settings=False):
self.name = account_name
hide_name = hide_name_settings
playerDict = {} #Pretty保存每个玩家的课程
那么我怎样才能获得每个后面都有“\ n”的播放器列表,其中hide_name不是True
答案 0 :(得分:1)
你不能,不像你的班级目前是定义的。但是,如果您将代码更改为:
class Player(object):
def __init__(self, account_name, hide_name=False):
self.name = account_name
self.hide_name = hide_name
然后它变得微不足道了:
players = {0: Player('Sir Lancelot', True),
1: Player('Sir Gallahad', False),
2: Player('Sir Robin', True),
3: Player('King Arthur', False),
}
players_to_show = [players[k] for k in players if not players[k].hide_name]
for player in players_to_show:
print(player.name)
虽然说实话,如果我试图隐藏姓名,我可能会这样做:
class Player(object):
def __init__(self, account_name, show_when_spectating=False):
self.name = account_name
self.show_when_spectating = show_when_spectating
spectators = [Player('Sir Lancelot', True),
Player('Sir Gallahad', True),
Player('Sir Robin', True),
Player('King Arthur', False),
]
shown_spectators = [player for player in players if player.show_when_spectating]