title: Optional Parameters in Python with List Append
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">&ldquo;Least Astonishment&rdquo; in Python: The Mutable Default Argument</a>  </p>
  </blockquote>
  
  
  
  <p>This is very odd, an optional list parameter in Python is persistent between function calls when using the .append() method.</p>
  
  <pre><code>def wtf(some, thing, fields=[]):
      print fields
      if len(fields) == 0:
          fields.append('hey');
      print some, thing, fields
  
  wtf('some', 'thing')
  wtf('some', 'thing')
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>[]
  some thing ['hey']
  ['hey'] # This should not happen unless the fields value was kept
  some thing ['hey']
  </code></pre>
  
  <p>Why does the "fields" list contain "hey" when it is a parameter? I know it's local scope because I can't access it outside the function, yet the function remembers its value.</p>