Let's say I have a c library that manipulates a world somehow.
I want to use this library with python. I want to be able to write simple python scripts that represent different scenarios of world management.
I have functions that create and destroy a world: void* create(void); int destroy(void* world);
Here is some python code:
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)
Now I want to add functions like: int set_worl开发者_开发知识库d_feature(void* world, feature_t f, ...); int get_world_feature(void* world, feature_t f, ...);
The thing is that in my python wrapper I don't know how to pass variously multiple arguments.
Because sometimes set_world_feature() is called with 3 or 4 arguments.
In Python again:
def set_world_feature(world, *features):
res = lib.set_world_feature(world, *features)
return world_error[res]
How to fix this in order for it to work?
When you do:
def create_world():
return _create
You don't call _create
, so create_world
returns the function pointer. If you want the pointer to your world instance you should write instead:
def create_world():
return _create()
精彩评论