title: How to properly subclass dict and override __getitem__ & __setitem__
body: <p>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 <code>dict</code> and implements a couple extra features. Anyway, what I would like to do is subclass <code>dict</code> myself and add override <code>__getitem__</code> and <code>__setitem__</code> to produce some debugging output. Right now, I have</p>
  <pre><code>class DictWatch(dict):
      def __init__(self, *args):
          dict.__init__(self, args)
  
      def __getitem__(self, key):
          val = dict.__getitem__(self, key)
          log.info(&quot;GET %s['%s'] = %s&quot; % str(dict.get(self, 'name_label')), str(key), str(val)))
          return val
  
      def __setitem__(self, key, val):
          log.info(&quot;SET %s['%s'] = %s&quot; % str(dict.get(self, 'name_label')), str(key), str(val)))
          dict.__setitem__(self, key, val)
  </code></pre>
  <p><code>'name_label'</code> 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 <code>DictWatch</code> instead of <code>dict</code> 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.</p>
  <p>Thanks for the help!</p>