Why variable still exists after function call finished -python
<p>I read from a book about some code like below, but it was not explained.
As you can see, before I call the function, no variable exists. 
But after function call, var2 was popped form stack and removed from our namespace of func_a as what we expected. But, var1 still exists!!!</p>

<blockquote>
  <p>How to explain this phenomenon? Is var1 a special kind of variable? </p>
</blockquote>

<pre><code>def func_a():
    func_a.var1 = 1
    var2 = 2

&gt;&gt;&gt; func_a.var1
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'function' object has no attribute 'var1'

&gt;&gt;&gt; var2
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'var2' is not defined

&gt;&gt;&gt; func_a()
&gt;&gt;&gt; func_a.var1
1
&gt;&gt;&gt; var2
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'var2' is not defined
</code></pre>

<blockquote>
  <p>How to explain this phenomenon? Is var1 a special kind of variable?</p>
</blockquote>

<p>Yes, <code>var1</code> is a special kind of variable. Or perhaps more precisely, it's not a variable at all. It's an attribute of an object (even though the object is a function). The object existed before the function call, and it continues to exist after. </p>

<p>In the function call you are <em>adding an attribute</em> to a global object rather than <em>creating a variable</em> in a local scope.</p>

python python-2.7