title: Intercepting __getitem__ calls on an object attribute
body: <p><strong>Question:</strong> How can I Intercept <code>__getitem__</code> calls on an object attribute?</p>
  
  <p><strong>Explanation:</strong></p>
  
  <p>So, the scenario is the following. I have an object that stores a dict-like object as an attribute. Every time the <code>__getitem__</code> method of this attribute gets called, I want to intercept that call and do some special processing on the fetched item depending on the key. What I want would look something like this:</p>
  
  <pre class="lang-python prettyprint-override"><code>class Test:
  
      def __init__(self):
          self._d = {'a': 1, 'b': 2}
  
      @property
      def d(self, key):
          val = self._d[key]
          if key == 'a':
              val += 2
          return val
  t = Test()
  assert(t.d['a'] == 3) # Should not throw AssertionError
  </code></pre>
  
  <p>The problem is that the @property method doesn't actually have access to the key in the <code>__getitem__</code> call, so I can't check for it at all to do my special postprocessing step. </p>
  
  <p>Important Note: I can't just subclass a MutableMapping, override the <code>__getitem__</code> method of my subclass to do this special processing, and store an instance of the subclass in <code>self._d</code>. In my actual code <code>self._d</code> is already a subclass of MutableMapping and other clients of this subclass need access to the unmodified data.</p>
  
  <p>Thanks for any and all help!</p>