Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume you are using Python 2? In Python 3, <code>C.m is C.m</code> (but <code>C().m is C().m</code> is still false). If you enter just <code>C.m</code> at the REPL, I bet you see something like <code>&lt;UnboundMethod... &gt;</code>. An UnboundMethod wrapper does very little, except checking <code>isinstance(self, cls)</code>. (Seems pretty pointless to create a wrapper for this? It is, so it was dropped in Python 3 - <code>C.m</code> is just a function). A fresh wrapper instance is created on-demand whenever the method is accessed - <code>C.m</code> creates one, another <code>C.m</code> creates another one. Since they're different instances, <code>C.m is not C.m</code>.</p> <p>Closely related are the bound methods, which allow you to do <code>f = obj.method; f(*args)</code> but also cause <code>instance.method is not instance.method</code>. Upon instanciation, all functions defined in the class (read: all methods, except of course monkeypatched ones) become properties of the instance. When you access them, you instead get a fresh instance of a wrapper (the bound method) around the plain function. This wrapper remembers the instance (<code>self</code>) and when called with <code>(arg1, arg2, ..., argN)</code> just hands these on to the function - with <code>self</code> added as first argument. You usually don't notice because you call the method right away - but this is what allows passing <code>self</code> implicitly without resorting to language-level trickery.</p> <p>See <a href="http://python-history.blogspot.com/2009/02/first-class-everything.html" rel="nofollow">the history of Python</a> for more details and, well, history.</p>
 

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