title: Python Simple Swap Function
body: <p>I came across this problem when attempting to learn python. Consider the following function:</p>
  
  <pre><code>def swap0(s1, s2):
      assert type(s1) == list and type(s2) == list
      tmp = s1[:]
      s1 = s2[:]
      s2 = tmp
  return
  
  s1 = [1]
  s2 = [2]
  swap0(s1, s2)
  print s1, s2
  </code></pre>
  
  <p>What will s1 and s2 print?</p>
  
  <p>After running the problem, I found that the print statement will print 1 2. It seems that the value of s1 and s2 did not change from the <code>swap0</code> function. The only explanation that I could think of was because of the line.</p>
  
  <pre><code>tmp = s1[:]
  </code></pre>
  
  <p>Since s1[:] is a copy, this makes sense that the value of s1 will not change in the function call. However because the parameter of <code>swap0</code> is (s1, s2), I am not sure if after doing tmp = s1[:]. Anytime I do</p>
  
  <pre><code>s1 = something...
  </code></pre>
  
  <p>it will be a reference to the copy of s1, instead of s1 itself. Can someone offer a better explanation? Thanks.</p>