【问题标题】:Is it possible to pop an item from dictionary, which is a property?是否可以从字典中弹出一个项目,这是一个属性?
【发布时间】: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


【解决方案1】:

您可以制作字典的本地副本并对其进行修改,但您不能修改函数hand_scores 中的值,因为它是在每次访问属性时创建的(因为每次都会调用该函数)。如果您希望能够对其进行修改,请将其改为普通的实例变量。

然后,为了确保变量得到更新,您可以让原始字典保存生成值的函数。

class Player:
    def __init__(self):
        self._hand_scores = {
            'Ones': lambda: YatzyHand.score_ones(self.hand),
            'Twos': lambda: YatzyHand.score_twos(self.hand),
            ...
        }

    @property
    def hand_scores(self):
        "Generate the dictionary of hand scores"
        return {key: valuefn() for key, valuefn in self._hand_scores.items()}

    def pop_hand_score(self, key):
        "Remove a score from hand_scores, returning its current value"
        return self._hand_scores.pop(key)()

所以字典中的值是在每次调用时生成的,但键保存在实例变量中。然后pop_hand_score 可以用来从实例中移除一个键,并返回它的最后一个值。

就我个人而言,我认为有一本这样的 lambda 字典是相当糟糕的风格,但它确实有效。

一种更简洁的技术(在我看来)还允许您将键放回原处,将一组键保留在实例变量中,将它们映射到函数的字典可以保留在函数中。

class Player:
    def __init__(self):
        self._hand_score_keys = {'Ones', 'Twos', ...}

    def hand_scores(self):
        # You could also make this a dict of actual values if you
        # don't mind calculating them even when you don't need them.
        score_functions = {
            'Ones': lambda: YatzyHand.score_ones(self.hand),
            'Twos': lambda: YatzyHand.score_twos(self.hand),
            ...
        }
        return {key: score_functions[key]() for key in self._hand_score_keys}

    def ignore_score(self, key):
        self._hand_score_keys.remove(key)

    def recall_hand_score(self, key): # needs a better name
        self._hand_score_keys.add(key)

【讨论】:

    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多