Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I will explain wat the line <code>id(A().a)</code> does:</p> <pre><code>A() # creates a new object I call a </code></pre> <p>Then</p> <pre><code>A().a # creates a function f bound to a A.a.__get__(A(), A) # same as above &gt;&gt;&gt; A.a.__get__(A(), A) &lt;bound method A.a of &lt;__main__.A object at 0x02D85550&gt;&gt; &gt;&gt;&gt; A().a &lt;bound method A.a of &lt;__main__.A object at 0x02D29410&gt;&gt; </code></pre> <p>This bound function is always another because it has another object in <code>__self__</code></p> <pre><code>&gt;&gt;&gt; a = A() &gt;&gt;&gt; assert a.a.__self__ is a </code></pre> <p><code>__self__</code> will be passed as first argument <code>self</code> to the function A.a</p> <p>EDIT: This is what it looks like:</p> <pre><code>Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. &gt;&gt;&gt; class A: def a(self): pass &gt;&gt;&gt; id(A().a) 43476312 &gt;&gt;&gt; id(A().a) 49018760 </code></pre> <p>Here the id repeats like abab</p> <pre><code>Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. &gt;&gt;&gt; class A: def a(self): pas s &gt;&gt;&gt; id(A().a) 50195512 &gt;&gt;&gt; id(A().a) 50195832 &gt;&gt;&gt; id(A().a) 50195512 &gt;&gt;&gt; id(A().a) 50195832 </code></pre> <p>EDIT: For linux or what is not my machine and whatsoever I do not know</p> <pre><code>id(A().a) </code></pre> <p>will always give the same result except if you store this to a variable. I do not know why but I would think that it is because of performance optimization. For objects on the stack you do not need to allocate new space for an object every time you call a function.</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