Cython:声明无符号整数的正确方法

hli*_*117 5 python numpy cython

在查看 scikit-learn 中的一些源代码时,我注意到tree.pxd以下一些类型声明:

import numpy as np
cimport numpy as np

ctypedef np.npy_float32 DTYPE_t          # Type of X
ctypedef np.npy_float64 DOUBLE_t         # Type of y, sample_weight
ctypedef np.npy_intp SIZE_t              # Type for indices and counters
ctypedef np.npy_int32 INT32_t            # Signed 32 bit integer
ctypedef np.npy_uint32 UINT32_t          # Unsigned 32 bit integer
Run Code Online (Sandbox Code Playgroud)

我知道这里的 Cython 文档有一些关于 C 类型和 cython 类型之间的区别的讨论,但这些似乎是来自 numpy 的类型,并且文档中没有提到它们。

我对应该使用什么类型感到困惑。对于索引,我应该使用SIZE_t上面定义的,还是unsigned int?这些真的有必要ctypedef存在吗?

hli*_*117 5

根据cython 的 numpy 的init .pxd 文件unsigned int,这似乎与npy_uint32.

另一方面,根据这一行npy_intp,与 是相同的Py_intptr_t文件的我很确定这意味着指针的大小,它对应于数组中项目之间的间距等。

我认为基于此讨论(无需提及)Py_intptr_t是首选,以适应架构之间的差异。因此,要真正确定架构适应性,npy_intp应该使用。