python memorize default parameter value or not
<p>From this blog, it seems Python memorize default parameter value during each function call.</p>

<p><a href="http://effbot.org/zone/default-values.htm" rel="nofollow">http://effbot.org/zone/default-values.htm</a></p>

<p>But I tested and it seems not? For example, the 2nd function call does not print value of x, y is still False other than memorized value?</p>

<pre><code>def foo(x, y=False):
    if y:
        print x

if __name__ == "__main__":
    foo(100, True) # print
    foo(300) # not print
</code></pre>

<p>regards,
Lin</p>

<p>The default object is <em>created</em> once.</p>

<p>Maybe this example will simplify the explanation:</p>

<pre><code>&gt;&gt;&gt; import random
&gt;&gt;&gt; 
&gt;&gt;&gt; def func(value=random.random()):
...     print value
...
&gt;&gt;&gt;
&gt;&gt;&gt; func()
0.941870977323
&gt;&gt;&gt; func()
0.941870977323
&gt;&gt;&gt; func(1)
1
&gt;&gt;&gt; func()
0.941870977323
&gt;&gt;&gt;
</code></pre>

<p>The default value is an object which is created when the function is defined. In my case, the created object was random value <code>0.941870977323</code>.</p>

<p>Every time the function is called without arguments, the <strong>same default value</strong> is used.</p>

<p>When it is called with an argument, then the defaut value is not used.</p>

<h2>When is it relevant?</h2>

<p>When the default is <code>False</code>, it does not matter, because <code>False</code> is immutable, so it does not matter whether it is an <em>old</em> <code>False</code>or a <em>new</em> <code>False</code>.</p>

<p>If the default is something that can change, it is important to understand that it is created only once:</p>

<ul>
<li>in case of random: it is a random value, but always the same random value</li>
<li>in case of <code>[]</code> an empty list is created once, but if it is modified by adding elements, the next call will not create a new empty list - it will use the same one.</li>
</ul>

python python-2.7