Python中尖括号的含义是什么? [英] What is the meaning of angle brackets in Python?

查看:123
本文介绍了Python中尖括号的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下几行 在 scikit-learn 包中:

I found the following lines in the scikit-learn package:

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 中的尖括号"的所有搜索都给出了关于文档装饰器语法,我很确定这两者都不是,因为它看起来像实际的逻辑.

All my searches for "angle brackets in Python" give answers about documentation or decorator syntax, which I am pretty sure this is neither because it looks like actual logic.

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

What do the angle brackets in the above Python code do and where can I learn more about them?

推荐答案

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

That is Cython's syntax for type casting/coercion. It is not plain Python. Notice the file extension is .pyx

您可以在文档中了解有关它们的更多信息 用于 Cython.

You can learn more about them in the documentation for Cython.

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

Here's an example taken from the doc page:

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

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

Using Cython is not uncommon with projects like scikit-learn, where one gains significant optimisations by mixing readable Python with blazing-speed C.

这篇关于Python中尖括号的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆