I have a class that inherits from a dictionary to add some kind of custom behavior - in this case it passes each key and the value of the function to be checked. In the example below, a "check" simply prints a message.
Dictionary assignment works as expected, printing messages whenever elements are added to the dict. But when I try to use a custom word type as an __dict__ attribute for a class, assigning attributes that in turn put keys / values ββinto my custom dictionary class, somehow I manage to insert values ββinto the dictionary, completely bypassing __setitem__ (and other methods that I determined which keys can add).
User Dictionary:
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
Using it as an attribute of the __dict__ class gives behavior that I don't understand.
>>> 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
Can someone explain why it is not behaving as I expect? Maybe he or he can't work the way I'm trying?
source share