title: How to change behavior of dict() for an instance
body: <p>So I'm writing a class that extends a dictionary which right now uses a method "dictify" to transform itself into a dict. What I would like to do instead though is change it so that calling dict() on the object results in the same behavior, but I don't know which method to override. Is this not possible, or I am I missing something totally obvious? (And yes, I know the code below doesn't work but I hope it illustrates what I'm trying to do.)</p>
  
  <pre><code>from collections import defaultdict
  
  class RecursiveDict(defaultdict):
      '''
      A recursive default dict.
  
      &gt;&gt;&gt; a = RecursiveDict()
      &gt;&gt;&gt; a[1][2][3] = 4
      &gt;&gt;&gt; a.dictify()
      {1: {2: {3: 4}}}
      '''
      def __init__(self):
          super(RecursiveDict, self).__init__(RecursiveDict)
  
      def dictify(self):
          '''Get a standard dictionary of the items in the tree.'''
          return dict([(k, (v.dictify() if isinstance(v, dict) else v))
                       for (k, v) in self.items()])
  
      def __dict__(self):
          '''Get a standard dictionary of the items in the tree.'''
          print [(k, v) for (k, v) in self.items()]
          return dict([(k, (dict(v) if isinstance(v, dict) else v))
                       for (k, v) in self.items()])
  </code></pre>
  
  <p>EDIT: To show the problem more clearly:</p>
  
  <pre><code>&gt;&gt;&gt; b = RecursiveDict()
  &gt;&gt;&gt; b[1][2][3] = 4
  &gt;&gt;&gt; b
  defaultdict(&lt;class '__main__.RecursiveDict'&gt;, {1: defaultdict(&lt;class '__main__.RecursiveDict'&gt;, {2: defaultdict(&lt;class '__main__.RecursiveDict'&gt;, {3: 4})})})
  &gt;&gt;&gt; dict(b)
  {1: defaultdict(&lt;class '__main__.RecursiveDict'&gt;, {2: defaultdict(&lt;class '__main__.RecursiveDict'&gt;, {3: 4})})}
  &gt;&gt;&gt; b.dictify()
  {1: {2: {3: 4}}}
  </code></pre>
  
  <p>I want dict(b) to be same as b.dictify()</p>