Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>millimoose gets the green check, but I thought I'd add a bit.</p> <p><strong>tl;dr</strong> </p> <p>Bound method objects are transient. That is, they are created anew each time you grab them.</p> <pre><code>class Foo(object): def bar(object): pass foo = Foo() m1 = foo.bar m2 = foo.bar print (id(m1)) print (id(m2)) # 38121264 # 38952752 </code></pre> <p><strong>More Detail: Descriptor Protocol</strong></p> <p>Bound method objects are created anew each time you grab them because the function object stored on the class implements the <a href="http://docs.python.org/howto/descriptor.html" rel="noreferrer">descriptor protocol</a>.</p> <p>The bar function is stored in the class dict:</p> <pre><code>class Foo(object): def bar(object): pass print (Foo.__dict__['bar']) # &lt;function bar at 0x00000000025F2208&gt; </code></pre> <p>When a Foo instance attempts to access bar, it doesn't have it. So it looks it up on its class and finds it in the class dict. The function object implements a <strong>__get__</strong> method, as per the descriptor protocol. So what is actually called to get the bound method is this:</p> <pre><code>print(Foo.__dict__['bar'].__get__(foo, Foo)) # &lt;bound method Foo.bar of &lt;__main__.Foo object at 0x00000000025719B0&gt;&gt; </code></pre> <p>Which is equivalent to:</p> <pre><code>print (foo.bar) # &lt;bound method Foo.bar of &lt;__main__.Foo object at 0x00000000025719B0&gt;&gt; </code></pre> <p>You can get at the original function like so:</p> <pre><code>print (foo.bar.im_func) # Python 2.x # &lt;function bar at 0x00000000025F2208&gt; print (foo.bar.__func__) # Python 3.x # &lt;function bar at 0x00000000025F2208&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload