title: How to dynamically change base class of instances at runtime?
body: <p><a href="http://www.linuxjournal.com/node/4540" rel="noreferrer">This article</a> has a snippet showing usage of <code>__bases__</code> to dynamically change the inheritance hierarchy of some Python code, by adding a class to an existing classes collection of classes from which it inherits.  Ok, that's hard to read, code is probably clearer:</p>
  
  <pre><code>class Friendly:
      def hello(self):
          print 'Hello'
  
  class Person: pass
  
  p = Person()
  Person.__bases__ = (Friendly,)
  p.hello()  # prints "Hello"
  </code></pre>
  
  <p>That is, <code>Person</code> doesn't inherit from <code>Friendly</code> at the source level, but rather this inheritance relation is added dynamically at runtime by modification of the <code>__bases__</code>attribute of the Person class.  However, if you change <code>Friendly</code> and <code>Person</code> to be new style classes (by inheriting from object), you get the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>TypeError: __bases__ assignment: 'Friendly' deallocator differs from 'object'
  </code></pre>
  
  <p>A bit of Googling on this seems to <a href="http://wiki.python.org/moin/NewClassVsClassicClass" rel="noreferrer">indicate some incompatibilities</a> between new-style and old style classes in regards to changing the inheritance hierarchy at runtime.  Specifically: <a href="http://www.python.org/download/releases/2.2.2/bugs/" rel="noreferrer">"New-style class objects don't support assignment to their <strong>bases</strong> attribute"</a>.</p>
  
  <p>My question, is it possible to make the above Friendly/Person example work using new-style classes in Python 2.7+, possibly by use of the <code>__mro__</code> attribute?</p>
  
  <p><strong><em>Disclaimer: I fully realise that this is obscure code.  I fully realize that in real production code tricks like this tend to border on unreadable, this is purely a thought experiment, and for funzies to learn something about how Python deals with issues related to multiple inheritance.</em></strong></p>