title: python: changing object class in multi-level inheritance
body: <p>I have looked at many questions posted here to find an answer to my problem, but I wasn't successful. The problem might be, that I just don't know for what keywords I should look. So my problem is the following:</p>
  
  <p>I've got a program, that has a multi-level inheritance and I am trying to figure out how the best way would be to change the class of an object to a subclass. Let's say I have the following code:</p>
  
  <pre><code>class A(object):
  
      def __init(self, filename, ..)
          super(A, self).__init__()
          ...some assignments here
  
  class B(A):
  
      def __init(self, filename, ..)
          super(B, self).__init__()
          ...some assignments here
  
  class C(A):
  
      def __init(self, filename, ..)
          super(C, self).__init__()
          ...some assignments here
  </code></pre>
  
  <p>and so on...<br>
  I always want to start initialising an object of class A. Depending on the type of the file that is used, the assignments may differ and depending on those assignments I can determine what kind of file it is. So now I want to change the class of the object to whatever class is suitable..</p>
  
  <p>I know I could pass the A object to B or C and use copy or deepcopy, but in A i am assigning an object of which the reference should not change and some others where it should change. Also I would need to delete that object of A, after initialising B or C.</p>
  
  <pre><code>class B(A):
  
      def __init__(self, filename, objA = None):
          if objA is not None:
              self.__dict__ = copy.deepcopy(objA.__dict__)
              del(objA)
          else:
              super(B, self).__init__(filename)
  </code></pre>
  
  <p>Also there is another possibility by changing the _<em>class</em> attribute to another class and use some kind of update method of the new class.<br>
  I would like to know, which of the two approaches is recommended or is there even a better one. Thanks in advance.</p>