【发布时间】:2019-11-12 13:21:21
【问题描述】:
我有一个小问题。我需要从字典中弹出项目,但字典是类的属性。
@property
def hand_scores(self):
return {'Ones': YatzyHand.score_ones(self.hand), 'Twos': YatzyHand.score_twos(self.hand),
'Threes': YatzyHand.score_threes(self.hand), 'Fours': YatzyHand.score_fours(self.hand),
'Fives': YatzyHand.score_fives(self.hand), 'Sixes': YatzyHand.score_sixes(self.hand),
'One Pair': YatzyHand.score_one_pair(self.hand),
'Two pairs': YatzyHand.score_two_pairs(self.hand),
'Three of a kind': YatzyHand.score_three_of_a_kind(self.hand),
'Four of a kind': YatzyHand.score_four_of_a_kind(self.hand),
'Yatzy': YatzyHand.score_yatzy(self.hand),
'Low straight': YatzyHand.score_low_straight(self.hand),
'High straight': YatzyHand.score_high_straight(self.hand),
'Full house': YatzyHand.score_full_house(self.hand),
'Chance': YatzyHand.score_chance(self.hand)}
我希望能够以这种方式弹出项目:
Player.pop('Chance')
我不知道该怎么办。
【问题讨论】:
-
该属性只返回一个字典。源自属性的事实无关紧要。
-
thing.hand_scores.pop('Fives') -
为什么不把这个字典作为类的变量,然后根据需要删除键? @juanpa.arrivillaga 是的,哎呀
-
只需在您的类上实现一个名为 pop 的方法,并根据需要让 pop 从其 dict 属性中删除该项目。不过,您(可能)希望将字典保存为类的属性。这是假设在你的类上有一个 pop 方法实际上很重要,它可能不是(pop 绝对是一个字典方法,除非你的类是字典,否则像
removeScore这样的更多描述会更有意义) -
你到底想做什么?您是否尝试从所有未来访问
hand_scores中删除该值?您是否尝试访问各个值?
标签: python oop dictionary properties