【问题标题】:Cython error: Cannot convert Python object to type 'int *' error, when passing an Integer array to a functionCython 错误:将整数数组传递给函数时,无法将 Python 对象转换为类型“int *”错误
【发布时间】:2013-03-28 11:09:11
【问题描述】:

我正在尝试将整数数组传递给 Cython 中的函数,但我不明白为什么会出现标题中提到的错误。

我正在尝试做的示例代码如下:

cpdef foo(int *table):
for i in range(10):
    table[i] = i

cdef int *temp=<int *>malloc(10*sizeof(int))
foo(temp)

for i in range(10):
    print temp[i]

我将不胜感激有关如何将数组正确传递给函数的任何指针。谢谢。

【问题讨论】:

    标签: python pass-by-reference cython


    【解决方案1】:

    这里的问题是函数是 cpdef - 这意味着它可以从 C 和 Python 代码中调用,并且意味着所有参数必须是 Python 对象(否则如何从 Python 中调用它?)

    改为cdef

    【讨论】:

      猜你喜欢
      • 2020-05-09
      • 2017-01-26
      • 2021-04-09
      • 2010-11-10
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多