title: Why the "mutable default argument fix" syntax is so ugly, asks python newbie
body: <p><sup>Now following <a href="https://stackoverflow.com/questions/2634091/python-some-newbie-questions-on-sys-stderr-and-using-function-as-argument/2635912#2635912">my series of "python newbie questions"</a> and based on <a href="https://stackoverflow.com/questions/575196/python-variable-scope-and-function-calls/575337#575337">another question</a>.</sup></p>
  
  <h2>Prerogative</h2>
  
  <p>Go to <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables" rel="nofollow noreferrer">http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables</a> and scroll down to "Default Parameter Values". There you can find the following:</p>
  
  <pre><code>def bad_append(new_item, a_list=[]):
      a_list.append(new_item)
      return a_list
  
  def good_append(new_item, a_list=None):
      if a_list is None:
          a_list = []
      a_list.append(new_item)
      return a_list
  </code></pre>
  
  <p>There's even an <a href="http://docs.python.org/tutorial/controlflow.html#default-argument-values" rel="nofollow noreferrer">"Important warning" on python.org</a> with this very same example, tho not really saying it's "better".</p>
  
  <h2>One way to put it</h2>
  
  <p>So, question here is: why is the <em>"good" syntax over a known issue</em> ugly like that in a programming language that <a href="http://wiki.python.org/moin/BeginnersGuide/Overview" rel="nofollow noreferrer">promotes "elegant syntax" and "easy-to-use"</a>?</p>
  
  <p><sup><em>edit:</em></sup></p>
  
  <h2>Another way to put it</h2>
  
  <p>I'm not asking <em><a href="https://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument">why or how it happens</a></em> (thanks Mark for the link).</p>
  
  <p>I'm asking <strong>why there's no simpler alternative built-in the language</strong>.</p>
  
  <p>I think a better way would probably being able to do something in the <code>def</code> itself, in which the name argument would be attached to a "local", or "new" within the <code>def</code>, mutable object. Something like:</p>
  
  <pre><code>def better_append(new_item, a_list=immutable([])):
      a_list.append(new_item)
      return a_list
  </code></pre>
  
  <p>I'm sure someone can come with a better syntax, but I'm also guessing there must be a very good explanation to why this hasn't been done.</p>