python (deI) statement and python behavior
<p>When a del statement is issued:</p>

<blockquote>
  <p>del var</p>
</blockquote>

<p>Shouldn't it be removed from the list of known variable and shouldn't the python interpreter spit out "unresolved reference" error?</p>

<p>Or is it simply just deleting the object and leaving the name (var) not pointing anywhere? Why would that kind of behavior be useful? In what cases?</p>

<p>Also I am talking simply about deleting a single variable. Not del list[3] or alike.</p>

<p>note: I am asking if this python's behavior is meant that way. And in what cases, it would be still useful.</p>

<p>EDIT: Charles Addis gave a detailed explanation. I also admit my mistake that I mistook the pycharm behavior as official python's. I am now trying out ipython along with official python interactive shell. Despite this being my mistake, I am glad that I learnt a lot about python variables along with some python debug commands.</p>

<p>Not sure what you're asking, as clearly thats what happens...</p>

<pre><code>&gt;&gt;&gt; x
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'x' is not defined
&gt;&gt;&gt; x = 10
&gt;&gt;&gt; 'x' in vars()
True
&gt;&gt;&gt; vars()['x']
10
&gt;&gt;&gt; del x
&gt;&gt;&gt; 'x' in vars()
False
&gt;&gt;&gt; x
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'x' is not defined
</code></pre>

<p>As you can see, Python stores valid identifiers in <code>vars()</code> and <code>locals()</code>... So del essentially removes it from memory, removing it from these data structures as well. So the identifier is no longer valid. It's not like in C where you might free some memory and set the value to <code>NULL</code>.</p>

<pre><code>&gt;&gt;&gt; x = 10
&gt;&gt;&gt; def func():
...   global x
...   del x
...
&gt;&gt;&gt; x
10
&gt;&gt;&gt; func()
&gt;&gt;&gt; x
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'x' is not defined
&gt;&gt;&gt;
</code></pre>

<h2>Update:</h2>

<p>Raymond made a good point when he mentioned that the object itself (read: the actual data in memory that the identifier points too, for lack of a more detailed explanation) is only freed when it's reference count goes to 0. IMO he could have done a better job detailing this in the python interpreter, so I'll have a go at it.</p>

<p>We'll use the ID function to prove this:</p>

<pre><code>Help on built-in function id in module __builtin__:

id(...)
    id(object) -&gt; integer

    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it's the object's memory address.)
</code></pre>

<p>Pay attention to whats going on here, you'll see that for immutable types, different values reference the same memory (strings are immutable in python and are interned, which means only one copy of each unique string is stored -- in ruby, symbols are interned strings).</p>

<pre><code>&gt;&gt;&gt; import sys
&gt;&gt;&gt; x = 10     # 10 is a common value, probably exists in memory already
&gt;&gt;&gt; sys.getrefcount(x)
26
&gt;&gt;&gt; id(x)      # memory location of x
140266396760096
&gt;&gt;&gt; y = x
&gt;&gt;&gt; id(y) == id(x)
True
&gt;&gt;&gt; z = 10
&gt;&gt;&gt; id(z) == id(y) == id(x)
True
&gt;&gt;&gt; sys.getrefcount(y)
28
&gt;&gt;&gt; sys.getrefcount(z)
28
&gt;&gt;&gt; del y, z
&gt;&gt;&gt; sys.getrefcount(x)
26
&gt;&gt;&gt; del x
&gt;&gt;&gt; x = 'charlie'
&gt;&gt;&gt; id(x)
4442795056
&gt;&gt;&gt; y = 'charlie'
&gt;&gt;&gt; z = x
&gt;&gt;&gt; id(x) == id(y) == id(z)
True
&gt;&gt;&gt; sys.getrefcount(x)
4
&gt;&gt;&gt; sys.getrefcount(y)
4
&gt;&gt;&gt; sys.getrefcount(z)
4
&gt;&gt;&gt; del y
&gt;&gt;&gt; del x
&gt;&gt;&gt; sys.getrefcount(z)     # will be two because this line is an additional reference
2
&gt;&gt;&gt; id(z)                  # pay attention to this memory location because this 
4442795056                 # is the last remaining reference to 'charlie', and
&gt;&gt;&gt; del z                  # when it goes out of scope 'charlie' is removed from
&gt;&gt;&gt;                        # memory.
&gt;&gt;&gt; id('charlie')          # This has a different memory location because 'charlie'
4442795104                 # had to be re-created.
</code></pre>

<p>First we set the identifier 'x' == 10, a common integer value. Since 10 is such a common value, it's almost guaranteed that something in the processes memory already has that value. Since integers are immutable in python, we only ever need one copy of each unique value stored in memory. In this case, there are 24 other references to 10 in memory. Setting <code>x = 10</code> creates the 25th reference, and calling <code>sys.getrefcount(x)</code> is the 26th reference (though it quickly goes out of scope). When we set <code>y = 10</code> and <code>z = x</code>, we know that they all point to the same data because they all have the same memory location. Calling <code>del</code> alters the reference count, but even when all 3 are deleted the integer 10 still exists in memory.</p>

<p>Next we create set <code>x = 'charlie'</code>, followed by <code>y = 'charlie'</code>, and finally <code>z = x</code>. You can see all of these variables have the same memory address. Once we delete all of these variables there are no more references to <code>'charlie'</code>. We can verify this by calling <code>id('charlie')</code> which will produce a different memory address meaning the string did not exist in memory when we called that function. </p>

<p>One more thing to note is the location of <code>'charlie'</code> and <code>10</code> in memory. <code>10</code> has a significantly higher memory address than Charlie does. This is because they exist in different locations of memory. <code>'charlie'</code> exists on the heap whereas <code>10</code> exists on the stack.</p>

<pre><code>&gt;&gt;&gt; hex(id(10))         # high address, this is on the stack
'0x7f9250c0b820'
&gt;&gt;&gt; hex(id('charlie'))  # lower address, this is on the heap
'0x108cfac60
</code></pre>

python