assignment of Array elements in ctypes Structure

Viewed 112

What's the idiomatic way of assigning array elements in a ctypes.Structure? For example, given:

from ctypes import Structure, c_float

class Foo(Structure):
    _fields_ = (
        ('arr', c_float*3),
    )

values = [1.1, 2.2, 3.3]

I expected to be able to just do:

foo = Foo()
foo.arr = values

but this complains about type mismatch.

TypeError: expected c_float_Array_3 instance, got list

I'm adding an answer showing what I've gone with, but would be interested to know of any more definitive answers.

1 Answers

The solution I've gone with is to use:

foo.arr = type(foo.arr)(*values)

While a more verbose option would be to do:

for i, v in enumerate(values):
  foo.arr[i] = v

Note that both options allow values to be shorter (i.e. zero to three elements), with missing entries padded with zeros. An error will be raised if the values contains too many entries. This is consistent with array initialization in C and happens to be what I want my code to do, but might be too liberal for other cases.

Related