【问题标题】:What is the meaning of angle brackets in Python?Python中尖括号的含义是什么?
【发布时间】:2017-08-24 10:15:47
【问题描述】:

我在 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:
    ...

我对“Python 中的尖括号”的所有搜索都给出了关于 documentationdecorator syntax 的答案,我很确定这两者都不是,因为它看起来像实际逻辑。

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

【问题讨论】:

    标签: python syntax brackets


    【解决方案1】:

    这是 Cython 的类型转换/强制语法。它不是普通的 Python。注意文件扩展名是.pyx

    您可以在 Cython 的 documentation 中了解有关它们的更多信息。

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

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

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

    【讨论】:

      【解决方案2】:

      看看Cython documentation, about types

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

      【讨论】:

        猜你喜欢
        • 2019-07-02
        • 2020-01-15
        • 2021-11-26
        • 1970-01-01
        • 1970-01-01
        • 2018-07-15
        • 2022-10-01
        • 2011-09-30
        • 2022-06-13
        相关资源
        最近更新 更多