Cython对进口有何作用? [英] What does Cython do with imports?

查看:133
本文介绍了Cython对进口有何作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Python扩展,我真的很喜欢使用Cython的想法.主要是获得有关它的更多知识并利用速度提高(如果有).我已经阅读了相当多的Cython文档,但我还不是计算机科学家,并且不具备深入的知识来理解底层基础知识,因此我提出以下问题的原因:

我只是想知道,如果我在开发的Python扩展中使用外部导入(例如,ORM或SQL库或任何其他第三方库)会发生什么情况?

Cython如何处理它?<​​/strong>

How does Cython handle it?

此导入的库/扩展是纯Python还是也已编译,是否有所不同?

处理此问题的正确方法是什么?

What is the right way to handle this?

谢谢.

PS.问题以粗体显示.

PS. Questions are typed in bold.

推荐答案

cython的目标是与python兼容,也就是说,您可以cythonize任何python代码,并且它将像以前一样工作.目前,大部分python代码已经可以使用了. Cython还允许您优化部分代码,并将其编译为更有效的C代码.

The goal of cython is to be compatible with python, i.e. that you can cythonize any python code and it will work as before. Currently, a large fraction of python code already works. Cython furthermore allows you to optimize parts of your code and compile it into more efficient C code.

这就是说,任何python导入都将保持默认状态.对它们的任何调用都将作为python命令发出.即使模块是用C编写的,cython也会通过python绕行调用模块的函数.如果要直接使用C库,则必须为其提供cython绑定. cython文档介绍了如何执行此操作.

That being said any python import will stay as they were by default. Any calls to them will be issued as python commands. Even if the module is written in C, cython will take the detour via python to call functions of the module. If you want to use a C library directly, you have to have cython bindings for them. The cython documentation explains how to do this.

通常,python充当监督者并处理模块/类的范围.如果一个模块中的代码调用任何python函数(或访问python变量),则python将根据调用者的范围来解析该调用.如果被调用的函数恰好在第二个模块中,则python很乐意使用它.调用者将只得到结果,而不必真正在乎其他函数是否在不同的模块中.因此,关键是python的作用域规则,该规则决定了调用哪个函数.

Generally, python acts as an overseer and handles the scopes of modules/classes. If code in one module calls any python function (or accesses a python variable), python will resolve the call according to the scope of the caller. If the called function happens to be in a second module, python is happily using it. The caller will just get the result and should not really care whether the other function was in a different module or not. The key is thus the scoping rules of python that decide which function is called.

这篇关于Cython对进口有何作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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