I have a structure with different datatype fields. I would like to iterate through the structure fields, check the datatype, and set the field with an appropriate value.
I have access to the size and offset of the field through the .size and .offset attribute of the field. How can I get the ‘type’ attribute of the field? Using type(value) does not print the ctypes datatype for the particular field. If I print value then I do see the ctypes datatype but there doesn’t seem to be an attribute to access this directly.
How can I access the type field descriptor directly?
from ctypes import *
class A(Structure):
_fields_ = [("one", c_long),
("two", c_char),
("three", c_byte)]
>>> A.one
<Field type=c_long, ofs=0, size=4>
>>> A.one.offset
0
>>> A.one.size
4
>>> type(A.one)
<class '_ctypes.CField'>
Ideally I would like to get the field type similar to the snippet below…
>>> A.one.type
c_long
Just use the
_fields_list: