title: Using dir() in Python recursively to read the hierarchy of a class or module and its descendants
body: <p>Here is an example of what I want to do</p>
  
  <pre><code>import mainmodule
  output=dir(mainmodule)
  output 
   ['module1'
   'module2'
    ...
     ]
  dir(module1.module2)
  ['class1',
  'class2',
  ...
  ]
  </code></pre>
  
  <p>Now assume that I want to write a recursive function that will go like this:</p>
  
  <pre><code>def printstructure(mod)
    output=dir(mod)
    for i in output:
       print (i)
       printstructure(mod+'.'+i)    #trying to concatenate here to obtain module1.module2
  </code></pre>
  
  <p>The above is going to fail because module is an object (type(mod) is module and i is a string</p>
  
  <p>The question is how do I get the child object (reference to it) when I know its name. Sorry if the question is naive I am learning OOP</p>