I tried converting C code snippet to Python by using Ctypes, but it doesn't work. The C code is as below:
struct A
{
struct B
{
// Empty
};
struct C
{
B b;
};
};
My Python code is:
from ctypes import Structure
class A(Structure):
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", A.B)] # Error: Unresolved reference 'A'
What am I doing wrong?
In python, a class is bound to its name after the entire class has been defined. A
does not exist for the duration of the definition. The ctypes
Structures and unions shows how to construct nested structures. Each is defined as an outer class separately and its the Structure._fields_
attribute that makes one subordinate to the other.
from ctypes import Structure
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", B)]
class A(Structure):
_fields_ = [("B", B), ("C", C)]