title: How to make the class constructor that returns the object of child class?
body: <p>I'm coding in Python. I have a <code>Base</code> class which contains several methods. There are some child classes; they may have their own methods. I want the <code>Base</code> class constructor to create the object not of the class itself but the object of one of the child classes depending on the argument.</p>
  <p>For example, guess our child classes are <code>Point</code>, <code>Line</code>, and <code>Plane</code>, all are inherited from <code>Base</code>, and the difference between them is set by the <code>dim</code> attribute of the <code>Base</code> class.</p>
  <pre><code>class Base():
      
      def __init__(self, dim, a, b):
          self.dim = dim
          self.a = a
          self.b = b
          
  class Point(Base):
      
      def __init__(self, a, b):
          super().__init__(1, a, b)
          
  class Line(Base):
      
      def __init__(self, a, b):
          super().__init__(2, a, b)
          
  class Plane(Base):
      
      def __init__(self, a, b):
          super().__init__(3, a, b)
  </code></pre>
  <p>If I explicitly create a <code>Point</code>, the object type will be <code>Point</code>:</p>
  <pre><code>pointA = Point(0, 0)
  type(pointA) # __main__.Point
  </code></pre>
  <p>But if I do the same through the <code>Base</code> constructor, the object will be of class <code>Base</code>:</p>
  <pre><code>pointB = Base(1, 0, 0)
  type(pointB) # __main__.Base
  </code></pre>
  <p>So I'd like to change this behavior and make the <code>Base</code> constructor return a <code>Point</code>, <code>Line</code> or <code>Plane</code> object if the <code>dim</code> attribute is equal to 1, 2 or 3 respectively. How can I do this?</p>
  <p><strong>EDIT:</strong>
  Based on this thread (<a href="https://stackoverflow.com/questions/28035685/improper-use-of-new-to-generate-class-instances">Improper use of __new__ to generate class instances?</a>) I overrid the <code>Base.__new__()</code> and got the following code:</p>
  <pre><code>class Base():
      
      def __new__(cls, a, b, dim):
          if dim == 1:
              return object.__new__(Point)
          elif dim == 2:
              return object.__new__(Line)
          elif dim == 3:
              return object.__new__(Plane)    
          
  class Point(Base):
      
      def __init__(self, a, b, dim):
          self.a = a
          self.b = b
          
  class Line(Base):
      
      def __init__(self, a, b, dim):
          self.a = a
          self.b = b
          
  class Plane(Base):
      
      def __init__(self, a, b, dim):
          self.a = a
          self.b = b
  </code></pre>
  <p>The code above works, but it requires an explicit setting of the <code>dim</code> argument even when I create a new <code>Point</code> instance. A non-identical set of arguments in <code>Base.__new__()</code> and <code>Point.__init__()</code> raises an error. How can I keep this behavior but remove <code>dim</code> from the <code>Point</code> constructor?</p>