How can I create a dict that sets a flag if it's been modified

Tim N. van der Leeuw tim.leeuwvander at nl.unisys.com
Thu Jan 12 05:00:53 EST 2006


Should the dict flag when the dict itself has been updated? Or also
when any of the items in the dict has been updated?

Say you have a dict consisting of lists...

The dict should be flagged as modified when an item is added; or when
an item is replaced (you call dict.__setitem__ with a key that already
exists).
This is clear, and easy to achieve with a simple wrapper or subclass.

But should the dict also be flagged as 'modified' when I append an item
to one of the lists that is in the dict?
>>> l = d['a']
>>> l.append('1')
Does that code mean that the dict should be flagged as 'modified' in
your use-case? or not?

If yes, then the only feasible way might be to pickle the dict. And if
repeated pickling of the same dict is not guaranteed to give the same
results, then perhaps pickling d.getitems() would give the right
results since, AFAIK, dict.getitems() is at least guaranteed to
maintain the same order given that A) The dict is not changed and B)
You're using the same Python version.

Right?




More information about the Python-list mailing list