这是 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/