title: python3: bind method to class instance with .__get__(), it works but why?
body: <p>I know if you want to add a method to a class instance you can't do a simple assignment like this:</p>
  
  <pre><code>&gt;&gt;&gt; def print_var(self): # method to be added
          print(self.var)
  &gt;&gt;&gt; class MyClass:
          var = 5
  &gt;&gt;&gt; c = MyClass()
  &gt;&gt;&gt; c.print_var = print_var
  </code></pre>
  
  <p>this indeed would cause <code>print_var</code> to behave like a normal function, so the <code>self</code> argument wouldn't have his typical meaning:</p>
  
  <pre><code>&gt;&gt;&gt; c.print_var
  &lt;function print_var at 0x98e86ec&gt;
  &gt;&gt;&gt; c.print_var()
  Traceback (most recent call last):
    File "&lt;pyshell#149&gt;", line 1, in &lt;module&gt;
      c.print_var()
  TypeError: print_var() takes exactly 1 argument (0 given)
  </code></pre>
  
  <p>In order to let the function be considered a method (i.e. to bind it to the instance), I used to use this code:</p>
  
  <pre><code>&gt;&gt;&gt; import types
  &gt;&gt;&gt; c.print_var = types.MethodType(print_var, c)
  &gt;&gt;&gt; c.print_var
  &lt;bound method MyClass.print_var of &lt;__main__.MyClass object at 0x98a1bac&gt;&gt;
  &gt;&gt;&gt; c.print_var()
  5
  </code></pre>
  
  <p>but I found that <code>.__get__</code> may also be used for this purpose:</p>
  
  <pre><code>&gt;&gt;&gt; c.print_var = print_var.__get__(c)
  &gt;&gt;&gt; c.print_var
  &lt;bound method MyClass.print_var of &lt;__main__.MyClass object at 0x98a1bac&gt;&gt;
  &gt;&gt;&gt; c.print_var()
  5
  </code></pre>
  
  <p>The problem here is that it just works, but I can't understand how and why. The documentation about <a href="http://docs.python.org/py3k/reference/datamodel.html?highlight=__get__#object.__get__" rel="noreferrer" title=".__get__"><code>.__get__</code></a> doesn't seem to help very much.</p>
  
  <p>I'd appreciate if someone could clarify this behaviour of python's interpreter.</p>