python - Cython:(为什么/何时)最好使用 Py_ssize_t 进行索引?

标签 python numpy indexing cython unsigned-integer

这是 this question 的后续事件.

(为什么/何时)最好使用 Py_ssize_t 进行索引?在 docs我刚刚发现

# Purists could use "Py_ssize_t" which is the proper Python type for
# array indices.

-> 这是否意味着在索引 NumPy/Cython 时总是 - 数组/ View 应该使用 Py_ssize_t

-> 是 Py_ssize_t e. G。一个 unsigned int 以便我不能使用 @cython.boundscheck(False)

最佳答案

Py_ssize_t 已签名。见 PEP 353 ,其中说 “引入了一种新类型 Py_ssize_t,其大小与编译器的 size_t 类型相同,但已签名。它将是 ssize_t 的类型定义(如果可用)。”

您应该使用 Py_ssize_t 进行索引。我在 Cython 文档中没有找到关于此的明确声明,但 Cython 开发人员 Stefan Behnel 在一封电子邮件 (https://groups.google.com/forum/#!topic/cython-users/brENF_M9zxM) 中说了这么多:

As a general remark, you are using ints as indices. You should use the Py_ssize_t type instead (or size_t, if you prefer an unsigned type) to properly accommodate for 64 bit architectures.

关于python - Cython:(为什么/何时)最好使用 Py_ssize_t 进行索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987390/

相关文章:

python - 散点图中的颜色代码数据框

Python:如果语句评估为真,则获取索引

python - 您将如何参数化 Dagster 管道以运行具有多个不同配置/ Assets 的相同实体?

python - 如何将计算字段添加到 Django 模型

python - Numpy reshape 产生不同的大小错误

python - 每次使用不同的切片范围沿不同的暗淡对数组求和

python - 通过已知序列从fasta文件中提取序列和 header

python - GUI 按钮调用函数的问题,提示用户在 GUI 中输入然后显示输出

python - 对一维数据使用什么聚类算法?

python - 属性错误浮点对象没有属性 'append'