title: Monkey-patching class with inherited classes in Python
body: <p>After reading the answers to <a href="https://stackoverflow.com/questions/3765222/monkey-patch-python-class">the question about monkey-patching classes in Python</a> I tried to apply the advised solution to the following case.</p>
  
  <p>Imagine that we have a module <code>a.py</code></p>
  
  <pre><code>class A(object):
      def foo(self):
          print(1)
  
  class AA(A):
      pass
  </code></pre>
  
  <p>and let us try to monkey patch it as follows. It works when we monkey patch class <code>A</code>:</p>
  
  <pre><code>&gt;&gt;&gt; import a
  &gt;&gt;&gt; class B(object):
  ...     def foo(self):
  ...             print(3)
  ... 
  &gt;&gt;&gt; a.A = B
  &gt;&gt;&gt; x = a.A()
  &gt;&gt;&gt; x.foo()
  3
  </code></pre>
  
  <p>But if we try the inherited class, it turns to be not patched:</p>
  
  <pre><code>&gt;&gt;&gt; y = a.AA()
  &gt;&gt;&gt; y.foo()
  1
  </code></pre>
  
  <p>Is there any way to monkey patch the class with all its inherited classes?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>For now, the best solution for me is as follows:</p>
  
  <pre><code>&gt;&gt;&gt; class AB(B, a.AA):
  ...     pass
  ... 
  &gt;&gt;&gt; a.AA = AB
  &gt;&gt;&gt; x = a.AA()
  &gt;&gt;&gt; x.foo()
  3
  </code></pre>
  
  <p>Any complex structure of <code>a.AA</code> will be inherited and the only difference between <code>AB</code> and <code>a.AA</code> will be the <code>foo()</code> method. In this way, we don't modify any internal class attributes (like <code>__base__</code> or <code>__dict__</code>). The only remaining drawback is that we need to do that for each of the inherited classes.</p>
  
  <p>Is it the best way to do this?</p>