title: How to bind a type's `__init__` or `__new__` to some other class instance?
body: <p>I was trying to set a type attribute to a class using the Python built-in <code>setattr</code>. I've declared <code>__new__</code> and <code>__init__</code> methods in the type to see what their parameters would be and surprisingly they're not being bound to receive the class instance. I've read the Python docs on <code>setattr</code> and descriptors and I've performed some tests in the interpreter and I haven't found a way to bind the type's <code>__new__</code> or <code>__init__</code> methods to the class instance.</p>
  <p>This is the code fragment I've been toying with:</p>
  <pre class="lang-py prettyprint-override"><code>T = type(&quot;T&quot;, (object,), {&quot;__new__&quot;: lambda cls: print(f&quot;T.__new__: {cls}&quot;) or object.__new__(cls), &quot;__init__&quot;: lambda self: print(f&quot;T.__init__: {self}&quot;)})
  
  T()
  # T.__new__: &lt;class '__main__.T'&gt;
  # T.__init__: &lt;__main__.T object at 0x7f970b667c10&gt;
  # &lt;__main__.T object at 0x7f970b667c10&gt;
  
  class A:
      pass
  
  setattr(A, &quot;T&quot;, T)
  
  A.T()
  # T.__new__: &lt;class '__main__.T'&gt;
  # T.__init__: &lt;__main__.T object at 0x7f970b6675e0&gt;
  # &lt;__main__.T object at 0x7f970b6675e0&gt;
  
  A().T()
  # T.__new__: &lt;class '__main__.T'&gt;
  # T.__init__: &lt;__main__.T object at 0x7f970b667ee0&gt;
  # &lt;__main__.T object at 0x7f970b667ee0&gt; 
  
  </code></pre>
  <p>Essentially I want to know how can I make T receive the instance of A in the <code>__new__</code> or <code>__init__</code> methods. I believe that I didn't fully understand how <code>setattr</code> actually works and I'm misusing it or the way to accomplish this behaviour is not related to <code>setattr</code> at all.</p>
  <p><em>PS.: Declaring T as a regular class changes nothing and declaring <code>T::__get__</code> changes nothing as well.</em></p>