C++ C++;嵌入式Python:在C++;到python类的方法

C++ C++;嵌入式Python:在C++;到python类的方法,c++,python,tuples,C++,Python,Tuples,可以将元组作为方法的参数传递,但只要我想将元组传递给类的方法,它就不起作用了(我在行“ret=PyEval_CallObject(method,args);”中得到一个运行失败的结果) 如果有人知道它为什么不起作用,非常感谢 使用的代码如下: enter code Python Code: class cVector: def __init__(self,msg): self.value = msg def ComputeNorm(self,vecData): #don

可以将元组作为方法的参数传递,但只要我想将元组传递给类的方法,它就不起作用了(我在行“ret=PyEval_CallObject(method,args);”中得到一个运行失败的结果) 如果有人知道它为什么不起作用,非常感谢

使用的代码如下:

enter code Python Code:

class cVector:
  def __init__(self,msg):
    self.value = msg
  def ComputeNorm(self,vecData):
    #don't use vecData for instance
    result = 12.
    return(result)

enter C++ Code

PyObject *ret, *mymod, *pclass, *method, *args, *object;
float retValue;

Py_Initialize();
PySys_SetPath(".");

// Module
mymod = PyImport_ImportModule("mModule8");
if (mymod == NULL){
  cout << "Can't Open a module:\n" ;
  Py_DECREF(mymod);
}

// Class
pclass = PyObject_GetAttrString(mymod, "cVector");
if (pclass == NULL) {
  Py_DECREF(pclass);
  cout << "Can't find class\n";
}

// Parameters/Values
args = Py_BuildValue("(f)", 100.0);
if (args == NULL) {
  Py_DECREF(args);
  cout << "Can't build argument list for class instance\n";
}

// Object with parameter/value
object = PyEval_CallObject(pclass, args);
if (object == NULL) {
  Py_DECREF(object);
  cout << "Can't create object instance:\n";
}

// Decrement the argument counter as we'll be using this again 
Py_DECREF(args);

// Get the object method - note we use the object as the object
// from which we access the attribute by name, not the class 
method = PyObject_GetAttrString(object, "ComputeNorm");
if (method == NULL) {
  Py_DECREF(method);
  cout << "Can't find method\n";
}

// Decrement the counter for our object, since we now just need
// the method reference 
Py_DECREF(object);

// Build our argument list - an empty tuple because there aren't
// any arguments 

cout << "Prepare the Tuple:\n" ;
// WE pass a tuple
args = PyTuple_New( 3 );
if (args == NULL) {
  Py_DECREF(args);
  cout << "Can't build argument list for method call\n";
}

PyObject  *py_argument;
// 1st argument 
py_argument = PyFloat_FromDouble(5.);
PyTuple_SetItem(args, 0, py_argument);

// 2nd argument 
py_argument = PyFloat_FromDouble(10.);
PyTuple_SetItem(args, 1, py_argument);

// 3nd argument 
py_argument = PyFloat_FromDouble(15.);
PyTuple_SetItem(args, 2, py_argument);

cout << "Before the Exec:\n" ;
// Call our object method with arguments 
ret = PyEval_CallObject(method,args);
//ret = PyObject_CallObject(method,args);
if (ret == NULL) {
  Py_DECREF(ret);
  cout << "Couldn't call method\n";
}


// Convert the return value back into a C variable and display it 
PyArg_Parse(ret, "f", &retValue);
printf("RetValue: %f\n", retValue);
// Kill the remaining objects we don't need 
Py_DECREF(method);
Py_DECREF(ret);
// Close off the interpreter and terminate 
Py_Finalize();  
输入代码Python代码:
类别cVector:
定义初始化(self,msg):
self.value=msg
def ComputeNorm(自身、矢量数据):
#例如,不要使用vecData
结果=12。
返回(结果)
输入C++代码
PyObject*ret、*mymod、*pclass、*method、*args、*object;
浮点数;
Py_初始化();
PySys_SetPath(“.”);
//模块
mymod=PyImport_ImportModule(“mModule8”);
如果(mymod==NULL){

cout您不显示如何获取
方法
。您必须从实例中获取该方法才能工作(这里我假设
inst
是一个
PyObject*
指向
cVector
类的实例):

始终检查错误:

if (method == NULL)
    return NULL;
if (ret == NULL)
   return NULL;
(或根据上下文做其他适当的事情)

然后,您的代码可以大大缩短:

PyObject *args = Py_BuildValue("(ddd)", 5.0, 10.0, 15.0);
(这将创建一个元组,其中包含三个由C double构成的Python浮点)

或者甚至结合电话:

PyObject *ret = PyObject_CallFunction(method, "(ddd)", 5.0, 10.0, 15.0);
您甚至可以在一次呼叫中组合所有内容:

PyObject *ret = PyObject_CallMethod(inst, "ComputeNorm",
                                    "(ddd)", 5.0, 10.0, 15.0);
同样,请记住检查错误:

if (method == NULL)
    return NULL;
if (ret == NULL)
   return NULL;
并且始终减少创建的所有对象的引用,不再需要这些对象(否则将泄漏内存):


(假设您使用了
PyObject\u CallMethod
,否则您可能还必须减少
args
方法的值)

“它不起作用”真的没有信息。怎么了?还有,你怎么初始化
args
?现在不能检查这个,但是记住元组是不可变的,你只能
PyTuple\u SetItem
和你在
PyTuple\u New
中声明的项目一样多。另外,你有没有尝试过,
PyObject\u CallFunction
,它支持一个漂亮的ode>PyObject\u BuildValue
-正确构建元组的风格?感谢您的简短回复抱歉,我没有按照我认为有效的方式编写完整的代码(方法、类初始化),代码如下(我想“动态地”初始化c++中的元组再次感谢您的快速回复,我不能在6小时之前将代码联机(来自stackoverflow的约束,对于第一次订户)如果您的解决方案不符合我的需要,即从数据库中为一个元组提供许多元素,我会尽快这样做。在这种情况下,代码段可以工作,但它可以在模块文件中使用直接方法,而不能在类中使用方法。您必须比“不工作”更具体(发布所有相关代码,说明发生了什么,说明你期望什么,发布完整的错误消息等)。谢谢Yak,我会在stackoverflow要求的6小时后第二天做你想做的事(新订阅者不能立即发布新消息,他必须等待6小时;