在Python中将多个参数传递给C函数

时间:2010-01-28 15:53:54

标签: python arguments ctypes

假设我有一个以某种方式操纵世界的c库。

我想在python中使用这个库。我希望能够编写代表世界管理不同场景的简单python脚本。

我有创造和摧毁世界的功能: void * create(void); int destroy(void * world);

这是一些python代码:

import ctypes

lib = ctypes.CDLL('manage_world.so')

_create = lib.create
_create.restype = ctypes.c_void_p

_destroy = lib.destroy
_destroy.argtypes = [ctypes.c_void_p,]
_destroy.restype = ctypes.c_int

def create_world():
    res =  _create()
    res = ctypes.cast(res, ctypes.c_void_p)
    return res

def destroy_world(world):
    return _destroy(world)

new_world = create_world()
print type(new_world)

print destroy_world(new_world)

现在我想添加以下功能: int set_world_feature(void * world,feature_t f,...); int get_world_feature(void * world,feature_t f,...);

问题在于我的python包装器中我不知道如何传递多个参数。

因为有时使用3或4个参数调用set_world_feature()。

再次在Python中:

def set_world_feature(world, *features):
    res = lib.set_world_feature(world, *features)
    return world_error[res]

如何解决此问题才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

当你这样做时:

def create_world():
    return _create

您不会调用_create,因此create_world会返回函数指针。如果你想要指向世界实例的指针,你应该写:

def create_world():
    return _create()
相关问题