如何正确子类化 dict 并覆盖 __getitem__ 和 __setitem__

问题描述 投票:0回答:6

我正在调试一些代码,我想找出何时访问特定的字典。嗯,它实际上是一个子类

dict
并实现了一些额外功能的类。不管怎样,我想做的是自己子类化
dict
并覆盖
__getitem__
__setitem__
以产生一些调试输出。现在,我已经

class DictWatch(dict):
    def __init__(self, *args):
        dict.__init__(self, args)

    def __getitem__(self, key):
        val = dict.__getitem__(self, key)
        log.info("GET %s['%s'] = %s" % str(dict.get(self, 'name_label')), str(key), str(val)))
        return val

    def __setitem__(self, key, val):
        log.info("SET %s['%s'] = %s" % str(dict.get(self, 'name_label')), str(key), str(val)))
        dict.__setitem__(self, key, val)

'name_label'
是最终将被设置的键,我想用它来识别输出。然后,我将正在检测的类更改为子类
DictWatch
而不是
dict
,并更改了对超级构造函数的调用。尽管如此,似乎什么也没有发生。我以为我很聪明,但我想知道我是否应该走不同的方向。

python dictionary inheritance subclass
6个回答
87
投票

子类化

dict
时的另一个问题是内置
__init__
不会调用
update
,内置
update
也不会调用
__setitem__
。因此,如果您希望所有 setitem 操作都通过您的
__setitem__
函数,您应该确保它被自己调用:

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

    def __getitem__(self, key):
        val = dict.__getitem__(self, key)
        print('GET', key)
        return val

    def __setitem__(self, key, val):
        print('SET', key, val)
        dict.__setitem__(self, key, val)

    def __repr__(self):
        dictrepr = dict.__repr__(self)
        return '%s(%s)' % (type(self).__name__, dictrepr)
        
    def update(self, *args, **kwargs):
        print('update', args, kwargs)
        for k, v in dict(*args, **kwargs).items():
            self[k] = v

45
投票

你正在做的事情绝对应该有效。我测试了你的类,除了你的日志语句中缺少左括号之外,它工作得很好。我能想到的只有两件事。首先,你的日志语句的输出设置是否正确?您可能需要在脚本顶部放置一个

logging.basicConfig(level=logging.DEBUG)

其次,

__getitem__
__setitem__
仅在
[]
访问期间被调用。因此,请确保您仅通过
DictWatch
访问
d[key]
,而不是
d.get()
d.set()


26
投票

考虑子类化

UserDict
UserList
。这些类旨在进行子类化,而普通的
dict
list
则不是,并且包含优化。


9
投票

这应该不会真正改变结果(对于良好的记录阈值,这应该有效): 你的init应该是:

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

相反,因为如果您使用 DictWatch([(1,2),(2,3)]) 或 DictWatch(a=1,b=2) 调用方法,则会失败。

(或者,更好的是,不要为此定义构造函数)


9
投票

正如 Andrew Pate 的回答所建议的,子类化

collections.UserDict
而不是
dict
更不容易出错。

这是一个示例,显示了天真继承时的问题:

dict

class MyDict(dict):

  def __setitem__(self, key, value):
    super().__setitem__(key, value * 10)


d = MyDict(a=1, b=2)  # Bad! MyDict.__setitem__ not called
d.update(c=3)  # Bad! MyDict.__setitem__ not called
d['d'] = 4  # Good!
print(d)  # {'a': 1, 'b': 2, 'c': 3, 'd': 40}

继承自

UserDict
,所以这按预期工作:
collections.abc.MutableMapping

同样,您只需实现
class MyDict(collections.UserDict): def __setitem__(self, key, value): super().__setitem__(key, value * 10) d = MyDict(a=1, b=2) # Good: MyDict.__setitem__ correctly called d.update(c=3) # Good: MyDict.__setitem__ correctly called d['d'] = 4 # Good print(d) # {'a': 10, 'b': 20, 'c': 30, 'd': 40}

即可自动兼容

__getitem__
key in my_dict
、…

注意:

my_dict.get

不是 
UserDict 的子类,因此 dict
 将失败(但 
isinstance(UserDict(), dict)
 可以工作)。

您所要做的就是

1
投票
isinstance(UserDict(), collections.abc.MutableMapping)

我个人使用的示例用法

class BatchCollection(dict): def __init__(self, inpt={}): super(BatchCollection, self).__init__(inpt)

注意
:仅在python3中测试

© www.soinside.com 2019 - 2024. All rights reserved.