问题
我想通过引用 Cython 中的函数来传递 vector 。
cdef extern from "MyClass.h" namespace "MyClass":
void MyClass_doStuff "MyClass::doStuff"(vector[double]& input) except +
cdef class MyClass:
...
@staticmethod
def doStuff(vector[double]& input):
MyClass_doStuff(input)
问题
上面的代码在编译期间没有抛出错误,但它也不起作用。 input
在方法之后没有变化。
我也试过推荐in this question但在这种情况下,cdef
函数将无法从 Python 访问(“未知成员 doStuff...”)。
是否可以通过引用传递,如果可以,如何正确执行?
编辑
这不是 cython-c-passing-by-reference 的拷贝正如我在上一节中提到的问题。提议的解决方案没有实现我的目标,即让 python 函数通过引用获取参数。
最佳答案
问题
正如 Kevin 和 jepio 在对您的问题的评论中所说,问题在于您如何在 Python 中处理 vector 。 Cython 确实定义了一个 cpp vector 类,它在边界处自动转换为列表/从列表转换为 Cython 代码。
问题在于转换步骤:当您的函数被调用时:
def doStuff(vector[double]& input):
MyClass_doStuff(input)
转化为接近
的东西def doStuff(list input):
vector[double] v= some_cython_function_to_make_a_vector_from_a_list(input)
MyClass_doStuff(input)
# nothing to copy the vector back into the list
答案
我认为您有两个选择。第一种是完整地写出流程(即手动复制两份):
def doStuff(list input):
cdef vector[double] v = input
MyClass_doStuff(v)
input[:] = v
这对于大 vector 来说会很慢,但对我有用(我的测试函数是 v.push_back(10.0)
):
>>> l=[1,2,3,4]
>>> doStuff(l)
>>> l
[1.0, 2.0, 3.0, 4.0, 10.0]
第二个选项是定义您自己的包装类,它直接包含一个vector[double]
cdef class WrappedVector:
cdef vector[double] v
# note the absence of:
# automatically defined type conversions (e.g. from list)
# operators to change v (e.g. [])
# etc.
# you're going to have to write these yourself!
然后写
def doStuff(WrappedVector input):
MyClass_doStuff(input.v)
关于python - Cython:通过引用传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29356403/