[Tutor] checksum of dictionary

Drew Perttula drewp@bigasterisk.com
Wed Feb 12 12:31:02 2003


> Hi,
> I have a time-consuming function that operates on a large dictionary. Before
> I call this function, I'd like to confirm that the dictionary has changed
> since the last time I called the function.
> To check for a change, I am thinking of calculating a checksum on the
> dictionary.

Nowadays you can subclass the dictionary to make one that notices any
changes. If you're purely concerned about setitem and delitem calls, you
could just override those. If you want to catch update(), etc, you might
like http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117236, which
is a dict interface defined in terms of getitem/setitem/delitem/keys. It's
great for constructing custom dict-like objects that use custom code
for all 20-or-so of the standard dict methods.

Obviously, watching dict accesses would be tremendously more efficient
than walking the whole dict every time you need to see if it changed.

-Drew