title: numpy array of objects
body: <p>I'm trying to implement a simulation for a lattice model (lattice boltzmann) in Python. Each site of the lattice has a number of properties, and interact with neighboring sites according to certain rules. I figured that it might be clever to make a class with all the properties and make a grid of instances of that class. (As I'm inexperienced with Python, this might not be a good idea at all, so feel free to comment on my approach.)</p>
  
  <p>Here is a toy example of what I'm doing</p>
  
  <pre><code>class site:
      def __init__(self,a,...):
          self.a = a
          .... other properties ...
      def set_a(self, new_a):
          self.a = new_a
  </code></pre>
  
  <p>Now I want to deal with a 2D/3D lattice (grid) of such sites so I tried to do the following (here is a 2D 3x3 grid as an example, but in simulation I would need the order of >1000x1000X1000)</p>
  
  <pre><code>lattice = np.empty( (3,3), dtype=object)
  lattice[:,:] = site(3)
  </code></pre>
  
  <p>Now, the problem is that each lattice point refer to the same instance, for example</p>
  
  <pre><code>lattice[0,0].set_a(5)
  </code></pre>
  
  <p>will also set the value of lattice[0,2].a to 5. This behavior is unwanted. To avoid the problem i can loop over each grid point and assign the objects element by element, like</p>
  
  <pre><code>for i in range(3):
      for j in range(3):
          lattice[i,j] = site(a)
  </code></pre>
  
  <p>But is there a better way (not involving the loops) to assign objects to a multidimensional array?</p>
  
  <p>Thanks</p>