I am debugging some code and I want to find out when a particular dictionary is accessed. Well, it's actually a class that subclass dict
and implements a couple extra features. Anyway, what I would like to do is subclass dict
myself and add override __getitem__
and __setitem__
to produce some debugging output. Right now, I have
class DictWatch(dict):
def __init__(self, *args):
dict.__init__(self, args)
def __getitem__(self, key):
val = dict.__getitem__(self, key)
log.info("GET %s['%s'] = %s" % str(dict.get(self, 'name_label')), str(key), str(val)))
return val
def __setitem__(self, key, val):
log.info("SET %s['%s'] = %s" % str(dict.get(self, 'name_label')), str(key), str(val)))
dict.__setitem__(self, key, val)
'name_label'
is a key which will eventually be set that I want to use to identify the output. I have then changed the class I am instrumenting to subclass DictWatch
instead of dict
and changed the call to the superconstructor. Still, nothing seems to be happening. I thought I was being clever, but I wonder if I should be going a different direction.
Thanks for the help!
dict.__init__
take*args
? – Tom Russell