Odd behaviour using a custom dict class as the __dict__ attribute of Python classes
我有一个从字典继承的类,以便添加一些自定义行为——在本例中,它将每个键和值传递给一个函数进行验证。在下面的示例中,"验证"只打印一条消息。
对字典的赋值按预期工作,在向字典中添加项时打印消息。但当我尝试使用自定义字典类型作为类的
自定义词典:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from collections import MutableMapping class ValidatedDict(dict): """A dictionary that passes each value it ends up storing through a given validator function. """ def __init__(self, validator, *args, **kwargs): self.__validator = validator self.update(*args, **kwargs) def __setitem__(self, key, value): self.__validator(value) self.__validator(key) dict.__setitem__(self, key, value) def copy(self): pass # snipped def fromkeys(validator, seq, v = None): pass # snipped setdefault = MutableMapping.setdefault update = MutableMapping.update def Validator(i): print"Validating:", i |
使用它作为类的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> d = ValidatedDict(Validator) >>> d["key"] ="value" Validating: value Validating: key >>> class Foo(object): pass ... >>> foo = Foo() >>> foo.__dict__ = ValidatedDict(Validator) >>> type(foo.__dict__) <class '__main__.ValidatedDict'> >>> foo.bar = 100 # Yields no message! >>> foo.__dict__['odd'] = 99 Validating: 99 Validating: odd >>> foo.__dict__ {'odd': 99, 'bar': 100} |
号
有人能解释一下为什么它不像我期望的那样表现吗?它能不能像我想的那样工作?
这是一个优化。为了支持