一种选择是通过指针参数返回值:
// c
void collect(int* outLatitude, int* outLongitude) {
*outLatitude = 10;
*outLongitude = 20;
}
和
# python
x = ctypes.c_int()
y = ctypes.c_int()
library.collect(ctypes.byref(x), ctypes.byref(y))
print x.value, y.value
如果你需要更多,你可以返回一个结构:
// c
typedef struct {
int latitude, longitude;
} Location;
Location collect();
和
# python
class Location(ctypes.Structure):
_fields_ = [('latitude', ctypes.c_int), ('longitude', ctypes.c_int)]
library.collect.restype = Location
loc = library.collect()
print loc.latitude, loc.longitude
顺便说一句:你提到了 Django;我会在这里小心并发。请注意,您的 C 库可能会从不同的线程中调用。