title: How to write container/wrapper class that adapts to member class interface?
body: <p>I'm writing a class that wraps around an object of another class. The intend is to change some of its method behaviors while able to extend all of its other interfaces. I'm not using inheritance because the inner class object can die and outer class needs to be able to replace it with a live one without destroying itself.</p>
  
  <p>so I have:</p>
  
  <pre><code>class Inner():
      def foo(): pass
      def goo(): pass
  
  class Outer():
      self.inner = InnerFactory(innerType)
      def foo(): 
           try:
               self.inner.foo() 
           except:
               del self.inner
               self.inner = InnerFactory(innerType)
               self.inner.foo()
  </code></pre>
  
  <p>The question is how to extend goo w/o explicitly rewrite as I may have tons of other such methods I'm not aware of. </p>
  
  <p>Actually after reading some of the feedbacks below, I realized I'm not using the great function <strong>getattr</strong>. However, I don't quite follow why the suggestions below all seem to use such a complicated version. Why can't it be as simple as:</p>
  
  <pre><code>def __getattr__( self, name ):
      if self.inner:
            return getattr( self.inner, name )
      else:
            raise Exception( 'attribute %s not found' % name ) 
  </code></pre>