I am playing around with ctypes a little bit to massage some C code I wrote into Python. The C code relies heavily on structs and unions, and for now, the quick solution in Python is to subclass those via ctypes:
I.e., from this:
struct foo {
uint32_t a;
uint32_t b;
uint16_t c;
uint16_t d;
};
To this:
from ctypes import *
class Foo(Structure):
_fields_ = [("a", c_uint),
("b", c_uint),
("c", c_ushort),
("d", c_ushort)]
Except, if I toss a __repr__() definition into the Python class and then use repr() on an instance, all I get back is <class 'Foo'> (or something to that effect, recalling from memory a bit here).
So I am wondering if there is a way to utilize repr() and try to get the best of both worlds between Python and C, or if I should look at metaclasses and using the struct library for packing/unpacking bytes into proper Python classes.
Thoughts?
I don’t really understand the problem.
This works just fine:
only if you do:
you’ll end up with
or something similar.
Are your sure you use
repron an instance?