【问题标题】:Error declaring function with arguments in cython [closed]在cython中使用参数声明函数时出错[关闭]
【发布时间】:2020-05-06 11:30:20
【问题描述】:

想出一种在 c++ 中使用 python 函数的方法,我遇到了 python,我正在编写一个简单的斐波那契函数,它将特定索引中的 n 个数字添加到给定的列表 l 中。我是 cython 的新手,所以错误可能是微不足道的:)。请指出有什么问题: 从 libcpp.list cimport 列表 从测试导入 test_sum

cdef public long long gen_fibonacci(list[int] &l,int in,int  n):
    num = 3
    t1 = 0 
    t2 = 1
    nextTerm = 0
    i=1
    if ind==1:
        l.append(0)
        l.append(1)
        i=3
    if ind==2:
        l.append(1)
        i=2
    while i<n:
        nextTerm=t1+t2
        t1=t2
        t2=nextTerm
        if num>=ind:
            i=i+1
            l.append(nextTerm)
        num=num+1
    return test_sum(l) 

错误:

cdef public long long gen_fibonacci(list[int] &l,int in,int  n):
                                                    ^
------------------------------------------------------------

strat_plugin.pyx:4:53: Expected ')', found 'in'

编译命令:

cython -2 strat_plugin.pyx

【问题讨论】:

  • in 是 Python 关键字。选择另一个名字。
  • 酷,谢谢,错误信息有点没用

标签: python c++ function cython


【解决方案1】:

尝试将in 换成该行中的另一个单词:

cdef public long long gen_fibonacci(list[int] &l,int in,int  n):` 

【讨论】:

    【解决方案2】:

    我认为您输入了“in”而不是“ind”。此外,'in' 是一个关键字,因此被保留。

    cdef public long long gen_fibonacci(list[int] &l,int ind,int n): num = 3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多