title: Subclassing, inheriting and subclass picking
body: <p>I would like to build a class and subclasses where the subclasses inherit some methods and from the superclass.</p>
  <p>Nothing special so far, but I would like to be able to create subclasses depending on a parameter e.g. <code>a = Shape(sides=3, base=2, height=12)</code> which should result in the same as <code>c = Triangle(base=2, height=2)</code> in the sample code:</p>
  <pre class="lang-py prettyprint-override"><code>class Shape:
      def __new__(cls, sides, *args, **kwargs):
          if sides == 3:
              print('a')
              return Triangle(*args, **kwargs)
          else:
              return Square(*args, **kwargs)
  
      def __init__(self, a):
          self._a = a
  
      @property
      def a(self):
          return self._a
  
  
  class Triangle(Shape):
      def __init__(self, base, height):
  
          super().__init__(self, a='triangle')
          self.base = base
          self.height = height
  
      def area(self):
          return (self.base * self.height) / 2
  
  
  class Square(Shape):
      def __init__(self, length):
          Shape.__init__(self, a='square')
          self.length = length
  
      def area(self):
          return self.length * self.length
  
  
  a = Shape(sides=3, base=2, height=12)
  b = Shape(sides=4, length=2)
  
  c = Triangle(base=2, height=2)
  
  print(c.a)
  
  print(str(a.__class__))
  print(a.area())
  
  print(str(b.__class__))
  print(b.area())
  </code></pre>
  <p>Which throws an error <code>TypeError: __new__() missing 1 required positional argument: 'sides'</code>.</p>
  <p>when I do not inherit the class (do <code>class Triangle:</code>) it down not throw the error but of course I cannot use the function <code>a</code> anymore...</p>
  <p>Any hints? My solutions so far were based on <a href="https://stackoverflow.com/a/60769071">https://stackoverflow.com/a/60769071</a>, <a href="https://stackoverflow.com/a/61509283">https://stackoverflow.com/a/61509283</a> and <a href="https://www.code-learner.com/how-to-use-python-__new__-method-example/" rel="nofollow noreferrer">https://www.code-learner.com/how-to-use-python-<strong>new</strong>-method-example/</a></p>
  <p>One possibility is maybe the use of a Factory pattern but I really liked the idea of overwriting the <strong>new</strong> function...</p>