title: Python - howto keep type of elements in list
body: <p>Sorry first to the pythonistas, C/C++ programmer having to do some python.</p>
  
  <p>I am porting some C (not C++), so I essentially want to create a list of structures to pass around for initialization, processing and writing on to a device over the serial port.</p>
  
  <p>I am keeping the python very close to the C so when the C is updated the python may easily get the same updates, so this means it looks procedural and there is some stuff I realize is nonsense, like creating an array, passing it off to be initialized and receiving it back (I understand that the one I create and pass in in is redundant)</p>
  
  <p>My problem is that when the array arrives to be processed, my list of recordtypes has become a flat list, so I can no longer accesses elements of my recordtype by name.</p>
  
  <p>I can cast them back (aka recreate each element from the list) but I would rather work out how to keep the type information intact.</p>
  
  <p>I have distilled the code to the following:</p>
  
  <pre><code>GainSettings = recordtype('GainSettings', 'label value')
  
  def init_arrays(Gains):
      labels = [ "Gain_1:", "Gain_2:", "Gain_48:" ]
      for itr in range(3):
          value = []
          value.extend([0] * CHANNELS)
          label = labels[itr]
          entry = GainSettings(label, value)
          Gains.extend(entry)
      return True, Gains
  
  def process_array(Gains):
      thing = Gains[0].label      # dies here with AttributeError: 'str' object has no attribute 'label'
      thing += "wibble"
      Gains[0].label = thing
      return True, Gains
  
  def main(args):
      # char*  label;
      # sint16 value[CHANNELS];
      Gains = []  # will be array of gain settings 
      ret, Gain = init_arrays(Gains)
      ret, Gains = process_array(Gains)
  </code></pre>
  
  <p>I have a breakpoint where it fails so I can check the array, this tells me:</p>
  
  <pre><code>&gt;&gt;&gt; print type(Gains)
  &lt;type 'list'&gt;
  &gt;&gt;&gt; print Gains
  ['Gain_1:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Gain_2:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'Gain_48:', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
  &lt;type 'list'&gt;
  &gt;&gt;&gt; print type(Gains[0])
  &lt;type 'str'&gt;
  &gt;&gt;&gt; print type(Gains[1])
  &lt;type 'list'&gt;
  </code></pre>
  
  <p>I would like it to say it is a list of "GainSettings".</p>
  
  <p>In reality there are a number of similar arrays, they have common items like label, the processing functions are supposed to take any type of array and use the similar parts to do the work.</p>
  
  <p>TIA</p>
  
  <p>Chris</p>