title: Default argument values calculated when function defined?
body: <p>I have difficutly understanding an example from a book (Introducing Python).</p>
  <p>First related example which I do understand:</p>
  <pre><code> &gt;&gt;&gt;def buggy(arg, result=[]):
   ...    result.append(arg)
   ...    print(result)
   ...
   &gt;&gt;buggy('a')
   ['a']
   &gt;&gt;&gt;buggy('b')
   ['a', 'b']
  </code></pre>
  <p>The example above illustrates that default argument values are calculated when the function is defined, not when it is run. This make sense to me.</p>
  <p>The next example shows how to avoid the issue. It works, but I do not understand how.</p>
  <pre><code>&gt;&gt;&gt;def nonbuggy(arg, result=None):
  ...    if result is None:
  ...        result = []
  ...    result.append(arg)
  ...    print(result)
  ...
  &gt;&gt;&gt;nonbuggy('a')
  ['a']
  &gt;&gt;&gt;nonbuggy('b')
  ['b']
  </code></pre>
  <p>My understanding (obviously wrong) is that when it is run for the first time, the result is None, therefore result = [] should be executed and 'a' appended.</p>
  <p>When run for the second time, result is not None (it is 'a'), so only 'b' should be appended and so I expect ['a', 'b'] to be returned. But I get only ['b'] exactly as it is in the book.</p>
  <p>Please could you advise how it is possible?</p>