Python ctypes,使用struct typedefs

Python ctypes,使用struct typedefs,python,ctypes,Python,Ctypes,假设我有以下c代码: typedef struct _test test; struct _test { test* just_a_test; char* just_a_char; }; 以下实施工作是否可行 class test(Structure): _fields_ = [ ('just_a_test', POINTER(test)), ('just_a_char', c_char_p), ] 我只是对结构中

假设我有以下c代码:

typedef struct _test test;

struct _test {
    test*    just_a_test;
    char*    just_a_char;
};
以下实施工作是否可行

class test(Structure):
    _fields_ = [
        ('just_a_test', POINTER(test)),
        ('just_a_char', c_char_p),
    ]

我只是对结构中的第一个指针感到困惑。

您的代码将无法工作,因为在它引用
test
时,类尚未创建

下面的
ctypes
文档中描述了此问题

解决方案是在创建类后设置
\u字段

class test(Structure):
    pass
test._fields_ = [
    ('just_a_test', POINTER(test)),
    ('just_a_char', c_char_p),
]

您的代码将无法工作,因为在它引用
test
时,类尚未创建

下面的
ctypes
文档中描述了此问题

解决方案是在创建类后设置
\u字段

class test(Structure):
    pass
test._fields_ = [
    ('just_a_test', POINTER(test)),
    ('just_a_char', c_char_p),
]