Understanding default variables in functions in Python
<p>I found this example in "Code Like Pythonista" by David Goodger at <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values" rel="nofollow">http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values</a>. I'm a little bit confused here. According to this example when bad_append() is called several times new_item will be appended to a_list. I don't understand why a_list will not be reset every time function is called. </p>

<pre><code>def bad_append(new_item, a_list=[]):
    a_list.append(new_item)
    return a_list

&gt;&gt;&gt; print bad_append('one')
['one']

&gt;&gt;&gt; print bad_append('two')
['one', 'two']
</code></pre>
