Python ctypedef在Cython和numpy中:什么是正确的约定?

Python ctypedef在Cython和numpy中:什么是正确的约定?,python,c,numpy,cython,Python,C,Numpy,Cython,在Cython中,当使用numpy时,写作的目的是什么: cimport numpy as np import numpy as np ctypedef np.int_t DTYPE_t 然后到处使用DTYPE\u t,而不是只使用np.int\u t?ctypedef在此处生成的代码中是否实际执行了不同的操作?您可以从中阅读注释,阅读注释他们解释了使用此符号的原因并导入 from __future__ import division import numpy as np # "cimport

在Cython中,当使用numpy时,写作的目的是什么:

cimport numpy as np
import numpy as np
ctypedef np.int_t DTYPE_t

然后到处使用
DTYPE\u t
,而不是只使用
np.int\u t
ctypedef
在此处生成的代码中是否实际执行了不同的操作?

您可以从中阅读注释,阅读注释他们解释了使用此符号的原因并导入

from __future__ import division
import numpy as np
# "cimport" is used to import special compile-time information
# about the numpy module (this is stored in a file numpy.pxd which is
# currently part of the Cython distribution).
cimport numpy as np
# We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime
# type info object.
DTYPE = np.int
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
# every type in the numpy module there's a corresponding compile-time
# type with a _t-suffix.
ctypedef np.int_t DTYPE_t
# "def" can type its arguments but not have a return type. The type of the
# arguments for a "def" function is checked at run-time when entering the
# function.
#
# The arrays f, g and h is typed as "np.ndarray" instances. The only effect
# this has is to a) insert checks that the function arguments really are
# NumPy arrays, and b) make some attribute access like f.shape[0] much
# more efficient. (In this example this doesn't matter though.)

我想这没关系,但这会让以后换成另一种类型更容易。他是对的。在任何地方编写
unsigned long long
之类的东西也可能有点麻烦。但这只是我的观点。假设我有pxd和pyx文件,它们都使用如下函数参数:np.ndarray[DTYPE\u double\u t,ndim=1]weight,但两个文件(pyx和pxd)中的ctypedef都会出现:“DTYPE\u int\u t”重新声明