title: Creating 2d array of classes in Python
body: <p>As part of a University exercise, we are being taken thru Python as in introductory programming language. I already know how to program to a reasonable standard, so I have been told that if I can present something of good quality, I can skip a bit of the class.</p>
  
  <p>To that extent, I have taken a C version of the text game Star Trek (<a href="http://www.dunnington.u-net.com/public/startrek/startrek.c" rel="nofollow">http://www.dunnington.u-net.com/public/startrek/startrek.c</a>) and rewritten it in Python, which went well enough, when I got used to the specific requirements of Python.</p>
  
  <p>My problem is something that seems to be a common one on Stackoverflow, which is the creation and use of a usable multidimensional array. After much searching, I have gotten the hang of creating multidimensional lists of integers or strings, etc, but when it comes to a multidimensional array (or list of lists, to be precise) of classes, all hell breaks loose.</p>
  
  <p>Here is my class:</p>
  
  <pre><code>class quadrant:
      klingonCount = 0
      baseHere = 0
      starCount = 0
  </code></pre>
  
  <p>And here is one of many attempts at creating a list of lists of classes:</p>
  
  <pre><code>quadrants = [quadrant] * 9
  for y1 in range(1,9):
      quadrants[y1] = [quadrant] * 9
  </code></pre>
  
  <p>This was copied from <a href="http://docs.python.org/2/faq/programming#how-do-i-create-a-multidimensional-list" rel="nofollow">http://docs.python.org/2/faq/programming#how-do-i-create-a-multidimensional-list</a></p>
  
  <p>As soon as I substituted a class for 0, printing the array showed that the entire array was referencing one class, not many different ones, as I had hoped.</p>
  
  <p>The problem is that the array only holds the values entered during the last iteration of the loop filling it out, not the individual ones.</p>
  
  <p>I'm not 100% familiar with the use of classes, so it may be that I'm just trying to reference one class many times, and I actually need many classes. Do I? Help!</p>
  
  <p>I'm using Python 2.7, which is the recommended language.</p>
  
  <p>For references sake, this is the code that attempts to populate the multidimensional list:</p>
  
  <pre><code>for x in range(1,9):
      for y in range(1,9):
  
          j = randint(1,99)
          if (j &lt; 5):
              numBases += 1
              baseHere = 1
  
          temp = getKlingons(difficulty)
  
          klingonsHere += temp
  
          quadrants[x][y].baseHere = baseHere
          quadrants[x][y].klingonCount = temp
          s = randint(1,9)
          quadrants[x][y].starCount = s
  </code></pre>