Python中的尖括号是什么意思?

What is the meaning of angle brackets in Python?

我在 scikit-learn 包中找到 the following lines

if is_sparse:
    problem = csr_set_problem(
            (<np.ndarray[np.float64_t, ndim=1, mode='c']>X.data).data,
            (<np.ndarray[np.int32_t,   ndim=1, mode='c']>X.indices).shape,
            (<np.ndarray[np.int32_t,   ndim=1, mode='c']>X.indices).data,
            (<np.ndarray[np.int32_t,   ndim=1, mode='c']>X.indptr).shape,
            (<np.ndarray[np.int32_t,   ndim=1, mode='c']>X.indptr).data,
            Y.data, (<np.int32_t>X.shape[1]), bias,
            sample_weight.data)
else:
    ...

我对 "angle brackets in Python" 的所有搜索都给出了关于 or decorator syntax 的答案,我很确定这两者都不是,因为它看起来像实际逻辑。

上面 Python 代码中的尖括号有什么作用,我在哪里可以了解更多关于它们的信息?

这是 Cython 对类型 casting/coercion 的语法。这并不简单Python。请注意文件扩展名为 .pyx

您可以在 documentation for Cython 中了解更多信息。

这是从文档页面中获取的示例:

cdef char *p, float *q
p = <char*>q

在像 scikit-learn 这样的项目中使用 Cython 并不少见,在这些项目中,通过将可读性 Python 与极速 C 相结合,可以获得显着的优化。

看看Cython documentation, about types

此外,您可能会注意到文件扩展名为 .pyx,文件顶部有 cimport 语句。