title: Recursively dump an object
body: <p>I am not sure if there is a standard way of doing this. I have implemented the following function to dump all contents of an object. It must recursively dump sub-objects, so I am checking for <code>InstanceType</code>, but it does not work:</p>
  
  <pre><code>import types
  
  def dump_obj(obj, level=0):
      for a in dir(obj):
          try:
              if type(obj.__dict__[a]) == types.InstanceType:
                  dump_obj(obj.__dict__[a], level + 2)
              else:
                  try:
                      print " " * level + "%s -&gt; %s" % (a, obj.__dict__[a])
                  except:
                      pass
          except:
              pass
  </code></pre>
  
  <p>How can I verify if an element is itself an object?</p>
  
  <p>What I actually want is the following. Given:</p>
  
  <pre><code>class B:
    def __init__(self):
      self.txt = 'bye'
  
  class A:
    def __init__(self):
      self.txt = 'hello'
      self.b = B()
  
  a = A()
  
  dump_obj(a)
  </code></pre>
  
  <p>I want the following output:</p>
  
  <pre><code>txt -&gt; hello
    txt -&gt; bye
  </code></pre>