Anyone know of any easy way to track changes to a dictionary object in python? I am at a high level doing crud, so I have a couple methods that handle changing a dictionary, if the dictionary changes I want to call a function to basically do an Observer/Notify.
class MyClass(object):
def update(self, item):
changed = False
if(self.my_dict.has_key(item.id)):
self.my_dict[item.id] = item
changed = True
if(changed):
self.notify()
What I am trying to avoid is all of the tracking(setting the boolean) code. Was hoping there was an easier way to track changes. This is a simple case, but there could have been more complicated logic that would result in me having to set the changed flag.
You can derive from the
dictclass and add a callback on any changes. This requires to overwrite any methods that change the dictionary: