Python - howto keep type of elements in list
<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>

<p>As I said in a comment, first of all, Python is not C. My suggestion is: take advantage of it, and use Python for your own good. Do not try to mimic C code. It will only make things harder.</p>

<p>On the other hand, you are flattening your list in an unexpected way. I think instead of:</p>

<pre><code>Gains.extend(entry)
</code></pre>

<p>You want:</p>

<pre><code>Gains.append(entry)
</code></pre>

<p>See the difference:</p>

<pre><code>&gt;&gt;&gt; from recordtype import recordtype
&gt;&gt;&gt; A = recordtype('A', 'x y')
&gt;&gt;&gt; x = A(1, 2)

# Extend
&gt;&gt;&gt; l = []
&gt;&gt;&gt; l.extend(x)
&gt;&gt;&gt; l
[1, 2]

# Append
&gt;&gt;&gt; l = []
&gt;&gt;&gt; l.append(x)
&gt;&gt;&gt; l
[A(x=1, y=2)]
</code></pre>

<p>The method <code>append()</code> adds an element to the list, while extend takes the recordtype as an iterable and, therefore, adds elements to the list for each of the records in the recordtype.</p>

<p>Python is a beautiful programming language. Don't waste this opportunity and try to learn some! ;-)</p>

python arrays recordtype