问题描述
我正在尝试用 C 语言做一个简单的扩展,它应该能够扩展 python 代码。 我在 https://github.com/munirhossain/py_c_extension
上找到了该代码#include <Python.h>
// Function 1: A simple 'hello world' function
static PyObject* helloworld(PyObject* self,PyObject* args)
{
printf("Hello Munir\n");
Py_RETURN_NONE;
return Py_None;
}
// Function 2: A C fibonacci implementation
// this is nothing special and looks exactly
// like a normal C version of fibonacci would look
int Cfib(int n)
{
if (n < 2)
return n;
else
return Cfib(n-1)+Cfib(n-2);
}
// Our Python binding to our C function
// This will take one and only one non-keyword argument
static PyObject* fib(PyObject* self,PyObject* args)
{
// instantiate our `n` value
int n;
// if our `n` value
if(!PyArg_ParseTuple(args,"i",&n))
return NULL;
// return our computed fib number
return Py_BuildValue("i",Cfib(n));
}
// Our Module's Function Definition struct
// We require this `NULL` to signal the end of our method
// definition
static PyMethodDef myMethods[] = {
{ "helloworld",helloworld,METH_NOARGS,"Prints Hello Munir" },{ "fib",fib,METH_VARARGS,"Computes Fibonacci" },{ NULL,NULL,NULL }
};
// Our Module Definition struct
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,"myModule","Test Module",-1,myMethods
};
// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
}
我想修改那个代码,就像我调用 helloworld func 一样,比如 helloworld("max")
它在 C 中返回 Hello max
,但是我如何使用 PyObject* args
:/
任何想法我怎么能做到这一点(在 C 中)?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)