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

Mike Meyer mwm at
Thu Jan 12 12:18:50 EST 2006

>> here's one attempt. (I'm no expert, so wait for better :-)
>>  >>> class ModFlagDict(dict):
>> 	def __init__(self, *args, **kwargs):
>> 		super(ModFlagDict, self).__init__(*args, **kwargs)
>> 		self.modified = False
>> 	def __setitem__(self, key, value):
>> 		self.modified = True
>> 		super(ModFlagDict, self).__setitem__(key, value)

You also need to catch __delitem__.

You may need to catch pop, update, clear and setdefault, depending on
whether or not they call the __setitem__/__delitem__. Personally, I'd
catch them all and make sure the flag got set appropriately, because
whether or not they use those methods is an implementation detail that
may change in the future.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list