0
在用Cython使用numpy的時候,什麼是寫作的一點:Cython與numpy ctypedef:什麼是正確的約定?
cimport numpy as np
import numpy as np
ctypedef np.int_t DTYPE_t
,然後使用DTYPE_t
隨處可見,而不是僅僅使用np.int_t
? ctypedef
實際上在結果代碼中做了什麼不同的事情嗎?
在用Cython使用numpy的時候,什麼是寫作的一點:Cython與numpy ctypedef:什麼是正確的約定?
cimport numpy as np
import numpy as np
ctypedef np.int_t DTYPE_t
,然後使用DTYPE_t
隨處可見,而不是僅僅使用np.int_t
? ctypedef
實際上在結果代碼中做了什麼不同的事情嗎?
您可以從docs for cython閱讀筆記,讀他們解釋了使用這種符號和進口的原因說明。
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.)
假設我有PXD和PYX文件,它們都使用函數的參數是這樣的:np.ndarray [DTYPE_double_t,NDIM = 1]的重量,但在ctypedef兩個文件(PYX和PXD)將發生:「DTYPE_int_t」重新聲明 – machen
我認爲無所謂,但它可以使它稍後更容易更改爲另一種類型。 – M4rtini
他是對的。在任何地方寫下'unsigned long long'這樣的東西也有點麻煩。這只是我的看法。 – IanH