title: Copying nested custom objects: alternatives to deepcopy
body: <p>I'm looking to make a deep copy of a class object that contains a list of class objects, each with their own set of stuff. The objects don't contain anything more exciting than ints and lists (no dicts, no generators waiting to yield, etc). I'm performing the deep copy on between 500-800 objects a loop, and it's really slowing the program down. I realize that this is already inefficient; it currently can't be changed.</p>
  
  <p>Example of how it looks:</p>
  
  <pre><code>import random
  import copy
  
  class Base:
      def __init__(self, minimum, maximum, length):
          self.minimum = minimum
          self.maximum = maximum
          self.numbers = [random.randint(minimum, maximum) for _ in range(length)]
          # etc
  
  class Next:
      def __init__(self, minimum, maximum, length, quantity):
          self.minimum = minimum
          self.maximum = maximum
          self.bases = [Base(minimum, maximum, length) for _ in range(quantity)]
          # etc
  </code></pre>
  
  <p>Because of the actions I'm performing on the objects, I can't shallow copy. I need the contents to be owned by the new variable:</p>
  
  <pre><code>&gt; first = Next(0, 10, 5, 10)
  &gt; second = first
  &gt; first.bases[0].numbers[1] = 4
  &gt; print(first.bases[0].numbers)
  &gt; [2, 4, 3, 3, 8]
  &gt; print(second.bases[0].numbers)
  &gt; [2, 4, 3, 3, 8]
  &gt;
  &gt; first = Next(0, 10, 5, 10)
  &gt; second = copy.deepcopy(first)
  &gt; first.bases[0].numbers[1] = 4
  &gt; print(first.bases[0].numbers)
  &gt; [8, 4, 7, 9, 9]
  &gt; print(second.bases[0].numbers)
  &gt; [8, 11, 7, 9, 9]
  </code></pre>
  
  <p>I've tried a couple of different ways, such as using json to serialize and reload the data, but in my tests it's not been nearly fast enough, because I'm stuck reassigning all of the variables each time. My attempt at pulling off a clever <code>self.__dict__ = dct</code> hasn't worked because of the nested objects.</p>
  
  <p>Any ideas for how to efficiently deep copy multiply-nested Python objects without using copy.deepcopy?</p>