title: Useful default __repr__ for nested class instances
body: <p>I have an abstract base class representing an interface.  Subclasses of this class store as properties other subclasses of this class.</p>
  
  <p>For example:</p>
  
  <pre><code>class AbstractBase(object):
      pass
  
  class Child(AbstractBase):
      def __init__(self, cls1, cls2):
          assert isinstance(cls1, AbstractBase)
          assert isinstance(cls2, AbstractBase) # just to show they're instances
  
          self.cls1 = cls1
          self.cls2 = cls2
  </code></pre>
  
  <p>The depth and layout of the hierarchy cannot be known in advance, but will not be recursive.</p>
  
  <p>What can I put as <code>__repr__</code> on <code>AbstractBase</code> that will allow me to see the proprties of each child class in a useful way?</p>
  
  <p>My current approach is:</p>
  
  <pre><code>from pprint import pformat
  
  class AbstractBase(object):
      def __repr__(self):
          return self.__class__.__name__ + '\n' \ 
                  + pformat({k:v for k,v in self.__dict__.iteritems()
                             if not '__' in k})
  </code></pre>
  
  <p>For a base class (with no properties which are subclasses of <code>AbstractBase</code>, this outputs something readable, eg:</p>
  
  <pre><code>MyClass
  {'var1': 1,
   'var2': 2}
  </code></pre>
  
  <p>However, for classes with <code>AbstractBase</code> subclasses it breaks, as it's hard to tell where a parent class starts and another ends (given that further levels of nesting aren't given further indenting by the <code>__repr__</code> above).</p>
  
  <p>I'd be happy with something like the below, imagining <code>cls1</code> and <code>cls2</code> had a single int property <code>var</code>:</p>
  
  <pre><code>Child
  {'cls1': {
            'var': 1,
           },
   'cls2': {
            'var': 0,
           }
  }
  </code></pre>
  
  <p>Sadly, I don't know how to achieve this (or if it's even possible).  Any thoughts?</p>