title: What's the most pythonic and elegant way to separate logic of a class and its parent if child class is determined by construction logic?
body: <p>I want to construct classes from strings such as <code>"red apple"</code>. This would create an instance of class <code>Apple</code>, which is a subclass of <code>Fruit</code>. The thing is, <code>color</code> attribute should belong to <code>Fruit</code>, not <code>Apple</code>. Therefore, the natural way of creating the object seem to me to be:</p>
  
  <ol>
  <li>parse the string</li>
  <li>create <code>Fruit(color="red")</code></li>
  <li>create <code>Apple()</code></li>
  <li>somehow make it a single entity</li>
  </ol>
  
  <p>So far I have 3 options:</p>
  
  <ol>
  <li><p>Everything becomes parameters</p>
  
  <pre><code>class Fruit(object):
      def __init__(self, color):
          self.color = color
  
      def observe(self):
          print "Looks like a tasty %s fruit" % self.color
  
      @classmethod
      def fromstring(cls, string):
          color, kind = string.split()
          if kind == "apple":
              return Apple(color)
  
  class Apple(Fruit):
      def __init__(self, *args, **kwargs):
          super(Apple, self).__init__(*args, **kwargs)
          self.tasty = True
  
      def bite(self):
          print "I bite into a tasty apple"
  
  fruit = Fruit.fromstring("red apple")
  </code></pre></li>
  <li><p>The <code>color</code> attribute is filled in from outside</p>
  
  <pre><code>class Fruit(object):
      def observe(self):
          print "Looks like a tasty %s fruit" % self.color
  
      @classmethod
      def fromstring(cls, string):
          color, kind = string.split()
          if kind == "apple":
              ins = Apple()
              ins.color = color
              return ins
  
  class Apple(Fruit):
      def __init__(self):
          self.tasty = True
  
      def bite(self):
          print "I bite into a tasty apple"
  
  fruit = Fruit.fromstring("red apple")
  </code></pre></li>
  <li><p>The most straightforward way: replacing <code>__class__</code></p>
  
  <pre><code>class Fruit(object):
      def __init__(self, string):
          self.color, kind = string.split()
          if kind == "apple":
              self.__class__ = Apple
              Apple.__init__(self)
  
      def observe(self):
          print "Looks like a tasty %s fruit" % self.color
  
  class Apple(Fruit):
      def __init__(self):
          self.tasty = True
  
      def bite(self):
          print "I bite into a tasty apple"
  
  fruit = Fruit("red apple")
  </code></pre></li>
  </ol>
  
  <p>Running</p>
  
  <pre><code>fruit.observe()
  fruit.bite()
  print type(fruit), fruit.tasty
  </code></pre>
  
  <p>gives the same output:</p>
  
  <pre><code>Looks like a tasty red fruit
  I bite into a tasty apple
  &lt;class '__main__.Apple'&gt; True
  </code></pre>
  
  <p>The first method, arguably the most versatile, requires passing around arguments such as <code>color</code>, which are handled much more elegantly in the third method. Yet altering <code>__class__</code> sounds like using advanced tools for a mundane task. Is there a better way to achieve the goal or I am better off using one of these things?</p>
  
  <p><strong>UPDATE</strong>: I probably must indicate that in real life the amount of attributes that <code>Fruit</code>'s and <code>Apple</code>'s initializers should set is <em>variable</em>, about 15 in total.</p>