Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer to that question lies in Python's distinction between unbound method and bound methods. When you define a method, it's <em>unbound</em>: it can operate on any instance of the class, and it is, in fact, nothing more than a function whose first argument is named self (and even then, it's a convention) and a little bit of checking so you don't do truly horrible things with them.</p> <pre><code>class Foo(object): def my_method(self, arg): print "self:", self print "arg:", arg a = Foo() print Foo.my_method(a, 2) # self: self: &lt;__main__.Foo object at 0x000000000219DE10&gt; # arg: 2 # You could, in theory, do that, but Python complains because it values the sanity # of the people who will have to maintain your code :p print Foo.my_method(1, 2) # TypeError: unbound method my_method() must be called with Foo instance as first argument (got int instance instead) </code></pre> <p>So far, nothing special. However, Python has a bit of magic when we call a method not on the class itself (as is done above), but on <em>an instance</em> of that class (that is, on an object).</p> <pre><code>a.my_method(2) # self: &lt;__main__.Foo object at 0x000000000219DE10&gt; # arg: 2 </code></pre> <p>Notice how suddenly you only have to supply <code>arg</code> to the method, even though it's defined with two arguments? That's because <code>a.my_method</code> is a <em>bound method</em>: it's the <code>my_method</code> method of the <code>a</code> <em>object</em>, not that of the <code>Foo</code> <em>class</em>. It is bound to that particular instance, and has a reference to that particular object in it (if you look at <code>dir(a.my_method)</code>, you'll notice it has a <code>im_self</code> attribute).</p> <p>However, in your example, if you were to add a <code>print self</code> statement in <code>func1</code>, it would print <code>foo0</code> instead of <code>foo0.child</code>. That's because the "binding" happens not when you <em>call</em> the method (<code>foo0.child.secondMethod()</code>), but simply when you reference it (<code>foo0.func1</code>).</p> <p>More importantly, it only ever happens when you reference a method that you defined on a class.</p> <p>So in your example, <code>foo0.child.secondMethod = foo0.func1</code> references <code>foo0</code>'s <code>func1</code> method (which gives you a bound method of <code>foo0</code>), but no magic is performed in the assignment itself. It's simply setting an attribute of <code>foo0.child</code>.</p> <p>Does that make sense?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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