title: Python references and loops
body: <p>I'm used to perl, where references and variables are explicitly separated.  Starting to use python makes life very confusing.  I want to work in a loop, creating objects and adding them to a list, but the results are frustrating.  I have read that functions define variable scope, but even encapsulating my loop code within functions doesn't seem to cut it.</p>
  <p>An example is as follows:</p>
  <pre class="lang-py prettyprint-override"><code>mylist = []
  
  class thing:
      def __init__(self, stuff=[]):
          self.stuff = stuff
  
  for i in range(10):
      obj = thing()
      obj.stuff.append(1)
      mylist.append(obj)
  
  len(mylist[0].stuff)
  </code></pre>
  <p>I would expect the length of the &quot;stuff&quot; list of each object to be 1, but it comes out as 10.  I know I can use deep copy to explicitly separate the newly created objects from what's gone before, but is there a more beautiful way?</p>