Reading the documentation here, I see that you can use ctypes.Structure as follows :-
>>> class Int(Structure):
... _fields_ = [("first_16", c_int, 16),
... ("second_16", c_int, 16)]
...
>>> print Int
Would it be possible for me to create such a class by using a list that I decide upon dynamically? For eg, something along the lines of :-
def int_factory(fields):
int_class = get_int_class(fields) # This returns a class object with _fields_ set to fields
return int_class
def user(values=[1,2]):
int_class = int_factory(int_fields)
i = int_class(values[0], values[1])
Would it be possible to do this?
In the example below:
int_factory generates the Int class
user returns an Int instance with first_16 set to 1, and second_16 set to 2
code00.py:
Output:
Might also check [SO]: Dynamically defining/updating ctypes structure in Python (@CristiFati's answer).