简体   繁体   English

Cython: cimport 和 import numpy as (both) np

[英]Cython: cimport and import numpy as (both) np

In the tutorial of the Cython documentation, there are cimport and import statements of numpy module:在 Cython 文档的教程中,有 numpy 模块的 cimport 和 import 语句:

import numpy as np
cimport numpy as np

I found this convention is quite popular among numpy/cython users.我发现这个约定在 numpy/cython 用户中非常流行。

This looks strange for me because they are both named as np .这看起来很奇怪的我,因为他们被命名为NP。 In which part of the code, imported/cimported np are used?代码的哪一部分使用了imported/cimported np? Why cython compiler does not confuse them?为什么 cython 编译器不会混淆它们?

cimport my_module gives access to C functions or attributes or even sub-modules under my_module cimport my_module允许访问C函数或属性,甚至my_module下的子模块

import my_module gives access to Python functions or attributes or sub-modules under my_module . import my_module给出下访问Python函数或属性或子模块my_module

In your case:在你的情况下:

cimport numpy as np

gives you access to Numpy C API, where you can declare array buffers, variable types and so on...使您可以访问 Numpy C API,您可以在其中声明数组缓冲区、变量类型等...

And:和:

import numpy as np

gives you access to NumPy-Python functions, such as np.array , np.linspace , etc使您可以访问 NumPy-Python 函数,例如np.arraynp.linspace

Cython internally handles this ambiguity so that the user does not need to use different names. Cython 在内部处理这种歧义,因此用户不需要使用不同的名称。

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

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