加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Cython中的引用

发布时间:2020-12-20 13:11:53 所属栏目:Python 来源:网络整理
导读:我在Cython中尝试使用引用时遇到了一些麻烦. 我正在使用此代码创建一个int并对其进行引用. cdef int i = 10;cdef int integer_ref = i; 但是,我无法弄清楚如何通过integer_ref更改i的值.在C中,我只使用integer_ref = some_other_value,这会将i的值设置为some
我在Cython中尝试使用引用时遇到了一些麻烦.
我正在使用此代码创建一个int并对其进行引用.

cdef int i = 10;
cdef int& integer_ref = i;

但是,我无法弄清楚如何通过integer_ref更改i的值.在C中,我只使用integer_ref = some_other_value,这会将i的值设置为some_other_value.

但是,在cython中执行此操作会返回错误,因为不允许分配给引用.
我也尝试了integer_ref [0] = some_other_value,但是它尝试索引int&这是不允许的.我该怎么用?
我发现this post on the cython-users list似乎暗示它是一个已知的bug,但我不太确定.使用(& integer_ref)[0] = some_other_value链接的变通方法对我来说也不起作用.

谢谢!

解决方法

您可以使用标准指针来解决此问题.例如,

def test():
    cdef:
        int i = 10
        int* ref = &i

    print(ref[0])  # Prints 10
    ref[0] = 5
    print(i)  # Prints 5

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读