Note that there are some explanatory texts on larger screens.

plurals
  1. POPython memory management insights -- id()
    primarykey
    data
    text
    <p>Playing around with <code>id()</code>. Began with looking at the addresses of identical attributes in non-identical objects. But that doesn't matter now, I guess. Down to the code:</p> <pre><code>class T(object): pass class N(object): pass </code></pre> <p>First test (in interactive console):</p> <pre><code>n = N() t = T() id(n) # prints 4298619728 id(t) # prints 4298619792 </code></pre> <p>No surprise here, actually. <code>n.__class__</code> is different than <code>t.__class__</code> so it seems obvious they can't possible be the same object. Is the <code>__class__</code> the <strong>only</strong> difference between these objects at this time? Assuming <strong>no</strong>, as:</p> <pre><code>&gt;&gt;&gt; n1 = N() &gt;&gt;&gt; n2 = N() &gt;&gt;&gt; id(n1) == id(n2) False </code></pre> <p>Or does Python simply create separate objects even if they are exactly the same, content-wise, instead of assigning the names <code>n1</code>, <code>n2</code> to, at first, the same object (in memory) and re-assign when either <code>n1</code> or <code>n2</code> is modified? Why so? I understand this may be a question of convention, optimization, mood, low-level issues (don't spare me) but still, I'm curious.</p> <p>Now, same classes as before, <code>T()</code> &amp; <code>N()</code> -- executed one after another in the shell:</p> <pre><code>&gt;&gt;&gt; id(N()) 4298619728 &gt;&gt;&gt; id(N()) 4298619792 &gt;&gt;&gt; id(N()) 4298619728 &gt;&gt;&gt; id(N()) 4298619792 </code></pre> <p>Why the juggling?</p> <p>But here comes the weird part. Again, same classes, shell:</p> <pre><code>&gt;&gt;&gt; id(N()), id(T()) (4298619728, 4298619728) &gt;&gt;&gt; id(N()), id(T()) (4298619728, 4298619728) &gt;&gt;&gt; id(N()), id(T()) (4298619728, 4298619728) </code></pre> <p>Not only the juggling stops, but N() and T() appear to be the same object. Since they cannot be, I understand this as whatever <code>N()</code> returns being destroyed after the <code>id()</code> call, <strong>before the end of the whole statement</strong>.</p> <p>I realize this may be a tough one to <strong>answer</strong>. But I'm hoping someone could tell me what I'm observing here, whether my understanding is correct, share some dark magic about the inner workings and memory management of the interpreter or perhaps point to some good resources on this subject?</p> <p>Thanks for your time on this one.</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