title: Python dictionary "plus-equal" behavior
body: <p>I'm trying to understand the exact mechanism behind updating a python dictionary using <code>d[key] += diff</code>.  I have some helper classes to trace magic method invocations:</p>
  
  <pre><code>class sdict(dict):
      def __setitem__(self, *args, **kargs):
          print "sdict.__setitem__"
          return super(sdict, self).__setitem__(*args, **kargs)
      def __delitem__(self, *args, **kargs):
          print "sdict.__delitem__"
          return super(sdict, self).__delitem__(*args, **kargs)
      def __getitem__(self, *args, **kargs):
          print "sdict.__getitem__"
          return super(sdict, self).__getitem__(*args, **kargs)
      def __iadd__(self, *args, **kargs):
          print "sdict.__iadd__"
          return super(sdict, self).__iadd__(*args, **kargs)
      def __add__(self, *args, **kargs):
          print "sdict.__add__"
          return super(sdict, self).__add__(*args, **kargs)
  
  class mutable(object):
      def __init__(self, val=0):
          self.value = val
      def __iadd__(self, val):
          print "mutable.__iadd__"
          self.value = self.value + val
          return self
      def __add__(self, val):
          print "mutable.__add__"
          return mutable(self.value + val)
  </code></pre>
  
  <p>With these tools, let's go diving:</p>
  
  <pre><code>&gt;&gt;&gt; d = sdict()
  &gt;&gt;&gt; d["a"] = 0
  sdict.__setitem__
  &gt;&gt;&gt; d["a"] += 1
  sdict.__getitem__
  sdict.__setitem__
  &gt;&gt;&gt; d["a"]
  sdict.__getitem__
  1
  </code></pre>
  
  <p>We don't see any <code>__iadd__</code> operation invoked here, which makes sense because the left-hand side expression <code>d["a"]</code> returns an integer that does not implement the <code>__iadd__</code> method.  We do see python magically converting the <code>+=</code> operator into <code>__getitem__</code> and <code>__setitem__</code> calls.</p>
  
  <p>Continuing:</p>
  
  <pre><code>&gt;&gt;&gt; d["m"] = mutable()
  sdict.__setitem__
  &gt;&gt;&gt; d["m"] += 1
  sdict.__getitem__
  mutable.__iadd__
  sdict.__setitem__
  &gt;&gt;&gt; d["m"]
  sdict.__getitem__
  &lt;__main__.mutable object at 0x106c4b710&gt;
  </code></pre>
  
  <p>Here the <code>+=</code> operator successfully invokes an <code>__iadd__</code> method. It looks like the <code>+=</code> operator is actually being used twice:</p>
  
  <ul>
  <li>Once for the magic translation to <code>__getitem__</code> and <code>__setitem__</code> calls</li>
  <li>A second time for the <code>__iadd__</code> call.</li>
  </ul>
  
  <p>Where I need help is the following:</p>
  
  <ul>
  <li>What is the exact, technical mechanism for translating the <code>+=</code> operator into <code>__getitem__</code> and <code>__setitem__</code> calls?</li>
  <li>In the second example, why is the <code>+=</code> operator used twice?  Doesn't python translate the statement to <code>d["m"] = d["m"] + 1</code>  (In which case wouldn't we see <code>__add__</code> be invoked instead of <code>__iadd__</code>?)</li>
  </ul>