gpt4 book ai didi

python - 覆盖子类中的 dict.update() 方法以防止覆盖字典键

转载 作者:太空狗 更新时间:2023-10-29 17:05:48 24 4
gpt4 key购买 nike

今天早些时候,我阅读了“Raise error if python dict comprehension overwrites a key”这个问题,并决定亲自尝试寻找答案。我自然想到的方法是为此将 dict 子类化。但是,我的回答卡住了,现在我痴迷于自己解决这个问题。

注意事项:

  • 否 - 我不打算将对这个问题的回答作为对另一个问题的回答。
  • 目前这对我来说纯粹是一种智力练习。实际上,只要我有这样的要求,我几乎肯定会使用 namedtuple 或常规字典。

我的(不是很有效)解决方案:

class DuplicateKeyError(KeyError):
pass



class UniqueKeyDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)


def __setitem__(self, key, value):
if key in self: # Validate key doesn't already exist.
raise DuplicateKeyError('Key \'{}\' already exists with value \'{}\'.'.format(key, self[key]))
super().__setitem__(key, value)


def update(self, *args, **kwargs):
if args:
if len(args) > 1:
raise TypeError('Update expected at most 1 arg. Got {}.'.format(len(args)))
else:
try:
for k, v in args[0]:
self.__setitem__(k, v)
except ValueError:
pass

for k in kwargs:
self.__setitem__(k, kwargs[k])

我的测试和预期结果

>>> ukd = UniqueKeyDict((k, int(v)) for k, v in ('a1', 'b2', 'c3', 'd4'))  # Should succeed.
>>> ukd['e'] = 5 # Should succeed.
>>> print(ukd)
{'a': 1, 'b': 2, 'c': 3, d: 4, 'e': 5}
>>> ukd['a'] = 5 # Should fail.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in __setitem__
__main__.DuplicateKeyError: Key 'a' already exists with value '1'.
>>> ukd.update({'a': 5}) # Should fail.
>>> ukd = UniqueKeyDict((k, v) for k, v in ('a1', 'b2', 'c3', 'd4', 'a5')) # Should fail.
>>>

我确定问题出在我的 update() 方法中,但我无法确定我做错了什么。

下面是我的 update() 方法的原始版本。当为字典中已有的键/值对调用 my_dict.update({k: v}) 时,此版本在重复项上按预期失败,但在创建原始键时包含重复键时不会失败dict,因为将 args 转换为 dict 会导致字典的默认行为,即覆盖重复键。

def update(self, *args, **kwargs):
for k, v in dict(*args, **kwargs).items():
self.__setitem__(k, v)

最佳答案

有趣的是,简单地覆盖 __setitem__ 不足以改变 dictupdate 的行为。我原以为 dict 在使用 update 更新时会使用它的 __setitem__ 方法。在所有情况下,我认为最好实现 collections.MutableMapping 以在不触及 update 的情况下实现所需的结果:

import collections

class UniqueKeyDict(collections.MutableMapping, dict):

def __init__(self, *args, **kwargs):
self._dict = dict(*args, **kwargs)

def __getitem__(self, key):
return self._dict[key]

def __setitem__(self, key, value):
if key in self:
raise DuplicateKeyError("Key '{}' already exists with value '{}'.".format(key, self[key]))
self._dict[key] = value

def __delitem__(self, key):
del self._dict[key]

def __iter__(self):
return iter(self._dict)

def __len__(self):
return len(self._dict)

编辑:包含 dict 作为基类以满足 isinstance(x, dict) 检查。

关于python - 覆盖子类中的 dict.update() 方法以防止覆盖字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30241688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com