python函数参数为类对象_将Cython类对象作为参数传递给C函数
問題已經解決了。這是因為類中的方法被定義為cdef,而不是{}。在
樣品c
#include "python.h"
void c_func(PyObject *obj){
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
/* Dictionary Object - can be of any dynamic type */
PyObject *dict_obj = PyDict_New();
PyObject *key = PyUnicode_FromString("average-run");
PyObject *val = PyLong_FromLong(45445);
PyDict_SetItem(dict_obj, key, val);
/* Make a callback */
PyObject *res = PyObject_CallMethod(obj, "func_a", "(O)", dict_obj);
if( !res )
{
PyErr_Print();
}else
{
Py_DECREF(res);
}
/*
* Note: Do not remove reference counting
* for callback argument - python will take
* care of it when the callback python method
* go out of scope. If you do - it will cause
* inconsistent data behaviour at the callback
* method side.
*/
PyGILState_Release(gstate);
}
pysample.pxd
^{pr2}$
pysample.pyx公司
cimport pysample
from pysample cimport Test
cdef class Test(object):
cpdef func_a(self, object dict_obj):
print(dict_obj)
def run_test(self):
# Here I make a call to the C function
pysample.c_func(self)
總結
以上是生活随笔為你收集整理的python函数参数为类对象_将Cython类对象作为参数传递给C函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python开发自动化创建一个任务下发到
- 下一篇: python创建脚本文件_python创