Say I have something like this (not tested):
class Foo(object):
def __init__(self):
self.store = {}
def __setitem__(self, key, value):
self.store[key] = value
print('Detected set')
def __getitem__(self, key):
return self.store[key]
__setitem__
is only called when the object itself is changed:
foo = Foo()
foo['bar'] = 'baz'
But it is not called, for example, when:
foo['bar'] = {}
foo['bar']['baz'] = 'not detected inside dict'
How can I detect this kind of case, or is there some other way I should be doing this? My goal is to have a dictionary-like object that is always in-sync with a file on disk.