Inner class cannot see the definition of another inner class with Python Ctypes
Question:
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'
Could you hint me what am I doing wrong? Thank you!
Answers:
I believe you have a typo in the code, specifically the _field_
part.
The problem is that it’s supposed to be _fields_
instead of _field_
!
from ctypes import Structure
class A(Structure):
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", A.B)]
Hope this helps!
Another answer
You can move C out of A
from ctypes import Structure
class A(Structure):
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", A.B)] # Error: Unresolved reference 'A'
this way, the C class can reference the A class because the A class has already been defined.
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)]
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'
Could you hint me what am I doing wrong? Thank you!
I believe you have a typo in the code, specifically the _field_
part.
The problem is that it’s supposed to be _fields_
instead of _field_
!
from ctypes import Structure
class A(Structure):
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", A.B)]
Hope this helps!
Another answer
You can move C out of A
from ctypes import Structure
class A(Structure):
class B(Structure):
_fields_ = []
class C(Structure):
_fields_ = [("b", A.B)] # Error: Unresolved reference 'A'
this way, the C class can reference the A class because the A class has already been defined.
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)]