简体   繁体   English

cdef在Cython中列出my_list

[英]cdef list my_list in Cython

I am confused about the following code that I see in a lot of places: 我对很多地方看到的以下代码感到困惑:

cdef list my_list

I am confused because list is not a C data type, but a Python data type. 我很困惑因为list不是C数据类型,而是Python数据类型。 Why would people use cdef instead of def then? 为什么人们会使用cdef代替def呢?

I like this feature a lot, because sometimes I need to use list in my code and it will take a tremendous amount of effort to restructure my code to C without a python list. 我非常喜欢这个功能,因为有时候我需要在我的代码中使用list,而且在没有python列表的情况下将代码重构为C需要付出巨大的努力。 I am just confused how and what Cython is doing, when a list was defined by cdef and have all the methods exposed to us. 我很困惑Cython正在做什么,当listcdef定义并且所有方法都暴露给我们时。

I tried to search the document, but without luck. 我试图搜索文件,但没有运气。 Any help would be appreciated! 任何帮助,将不胜感激!

cdef ing a Python data type just allows Cython to take a few (small) shortcuts and to type-check. cdef一个Python数据类型只允许Cython采取一些(小)快捷方式和类型检查。

This will not result in significantly faster code, as the Python overhead still exists. 这不会导致代码明显加快,因为Python开销仍然存在。 For convenient data types you'll wanting to be using numpy.array or cpython.array.array typed to a memoryview. 对于方便的数据类型,您希望使用numpy.arraycpython.array.array键入内存视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM