How can I make a dictionary that marks itself when it's modified?

Steve Holden steve at
Thu Jan 12 19:28:15 EST 2006

Christian Tismer wrote:
> Just to add a word that I forgot:
> Adhering to the subject line, the intent is to track modifications
> of a dict.
> By definition, modification of a member of a dict without replacing
> the value is not considered a dict change.
Well, I agree. But I suppose much depends on exactly what the OP meant 
by "... add a new element or alter an existing one". The post did follow 
that with "(the values in the dict are mutable)", which is presumably 
why garabik-2500 proposed catching __getitem__ as well as __setitem__.

I merely wanted to point out (not to you!) that there was no effective 
way to capture a change to a mutable item without, as you say, modifying 
the element classes.

> I'd stick with the shallow approach.
> Asking to track mutation of an element in the general case
> is causing much trouble.
> Support for element tracking can probably provided by overriding
> the dict's getattr and recording the element in some extra
> candidate list.
> If the element itself is modified, it then could be looked up
> as a member of that dict, given that the element's setattr
> is traced, too.
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC           
PyCon TX 2006        

More information about the Python-list mailing list