title: correctly override __new__ in python3
body: <p>So I am trying to override <code>__new__</code> and let it exist as a factory to create
  derived instances. After reading a bit on SO, I am under the impression that I should be calling <code>__new__</code> on the derived instance as well.</p>
  
  
  
  <p><strong>BaseThing</strong></p>
  
  <pre><code>class BaseThing:
      def __init(self, name, **kwargs):
          self.name = name
  
      # methods to be derived
  </code></pre>
  
  <p><strong>ThingFactory</strong></p>
  
  <pre><code>class Thing(BaseThing):
      def __new__(cls, name, **kwargs):
          if name == 'A':
             return A.__new__(name, **kwargs)
          if name == 'B':
             return B.__new__(name, **kwargs)        
  
      def __init__(self, *args, **kwargs):
          super().__init__(name, **kwargs)
  
     # methods to be implemented by concrete class (same as those in base)
  </code></pre>
  
  <p><strong>A</strong></p>
  
  <pre><code>class A(BaseThing):
      def __init__(self, name, **kwargs):
         super().__init__(name, **kwargs)
  </code></pre>
  
  <p><strong>B</strong></p>
  
  <pre><code>class B(BaseThing):
      def __init__(self, name, **kwargs):
         super().__init__(name, **kwargs)
  </code></pre>
  
  
  
  <p>what I am expecting was that it'd just work.</p>
  
  
  
  <pre><code>&gt;&gt;&gt; a = Thing('A')
  </code></pre>
  
  <p>gives me <code>TypeError: object.__new__(X): X is not a type object (str)</code></p>
  
  <p>I am bit confused by this; when I just return a concrete instance of derived classes, it just worked. i.e.</p>
  
  <pre><code>def __new__(cls, name, **kwargs):
          if name == 'A':
             return A(name)
          if name == 'B':
             return B(name)
  </code></pre>
  
  <p>I however don't think this it the correct way of returning in <code>__new__</code>; it may duplicate the calls to <code>__init__</code>.</p>
  
  <p>when I am checking signatures of <code>__new__</code> in <code>object</code> it seems be this one:</p>
  
  <pre><code>@staticmethod # known case of __new__
  def __new__(cls, *more): # known special case of object.__new__
      """ Create and return a new object.  See help(type) for accurate signature. """
      pass
  </code></pre>
  
  <p>I didn't expect this was the one; I'd expect it came with args and kwargs as well. I must have done something wrong here. </p>
  
  <p>it seems to me that I need to inherit object directly in my base but could  anyone explain the correct way of doing it?</p>