title: Transform a class with other calsses as attributes in a dictionary
body: <p>I am recreating a structure of a json file using classes and classes as attributes of the parent class (this allows me to use set methods for easing the process):</p>
  <pre><code>class A(object):
      def __init__(self,):
          self.test = &quot;something of A&quot;
      def set_test():
          pass # some function here to set test from B
  
  
  
  class B(object):
      def __init__(self,):
          self.test2 = A() # This is the A class that I wan't to access
          self.test3 = &quot;something of B&quot;
  
  
  
  g = B()
  g.__dict__
  </code></pre>
  <p>The result is this:</p>
  <pre><code>{'test2': &lt;__main__.A at 0x7fda2d2fd9a0&gt;, 'test3': 'something of B'}
  </code></pre>
  <p>I would instead want to get the dict object also of the class as an attribute:</p>
  <pre><code>{'test2': {'test': 'something of A'}, 'test3': 'something of B'}
  </code></pre>
  <p>I know that I can do: <code>g['test2'] = g['test2'].__dict__</code> but I was wondering if there is an automated way to get resolve as dictionaries all the classes linked as attributes.</p>
  <blockquote>
  <p>NOTE: I want to be able to access the methods of A from B otherwise I
  would have directly created a dictionary for B</p>
  </blockquote>