cython,加速python,保护代码(3):扩展文件 .pxd
来源:互联网 发布:mac apowersoft 编辑:程序博客网 时间:2024/06/06 05:25
http://docs.cython.org/src/tutorial/pure.html
.pxd文件相当于c语言中的头文件.h。
In addition to the .pyx source files, Cython uses .pxd files which work like C header files – they contain Cython declarations (and sometimes code sections) which are only meant for inclusion by Cython modules. A pxd file is imported into a pyx module by using the cimport keyword.
Using an augmenting .pxd allows to let the original .py file completely untouched. On the other hand, one needs to maintain both the .pxd and the .py to keep them in sync.
While declarations in a .pyx file must correspond exactly with those of a .pxd file with the same name (and any contradiction results in a compile time error, see pxd files), the untyped definitions in a .py file can be overridden and augmented with static types by the more specific ones present in a .pxd.
If a .pxd file is found with the same name as the .py file being compiled, it will be searched for cdef classes and cdef/cpdef functions and methods. The compiler will then convert the corresponding classes/functions/methods in the .py file to be of the declared type. Thus if one has a file A.py:
and adds A.pxd:
then Cython will compile the A.py as if it had been written as follows:
Notice how in order to provide the Python wrappers to the definitions in the .pxd, that is, to be accessible from Python,
具体规则:
Python visible function signatures must be declared as cpdef (with default arguments replaced by a * to avoid repetition):
C function signatures of internal functions can be declared as cdef:
cdef classes (extension types) are declared as cdef class;
cdef class attributes must be declared as cdef public if read/write Python access is needed, cdef readonly for read-only Python access, or plain cdef for internal C level attributes;
cdef class methods must be declared as cpdef for Python visible methods or cdef for internal C methods.
In the example above, the type of the local variable a in myfunction() is not fixed and will thus be a Python object. To statically type it, one can use Cython’s@cython.locals decorator (see Magic Attributes, and Magic Attributes within the .pxd).
Normal Python (def) functions cannot be declared in .pxd files. It is therefore currently impossible to override the types of plain Python functions in .pxd files, e.g. to override types of their local variables. In most cases, declaring them as cpdef will work as expected.
- cython,加速python,保护代码(3):扩展文件 .pxd
- Cython,加速python,保护代码(1):Overview
- cython,加速python,保护代码(2):Faster code via static typing
- 使用cython保护python的代码
- 使用Cython来保护Python代码库
- Python扩展和嵌入: Cython三分钟入门(笔记)
- Python扩展和嵌入: Cython三分钟入门(笔记)
- 用Cython编译Python的C扩展
- 用Cython编译Python的C扩展
- 【Cython】用Cython包装C++代码,提供给python调用
- 用cython做python3代码保护--编译成c代码
- Cython代码和Python代码区别
- Cython 0.15,用 OpenMP 并行多核加速 Python!
- Cython 0.15,用 OpenMP 并行多核加速 Python!
- 利用cython将python转为c代码
- 【转载】用Cython编译Python的C扩展
- 快速教程:使用Cython来扩展Python/NumPy库
- 【Cython】Cython包装C文件
- 20. 利用Veeam ONE监控vSphere虚拟化平台(Veeam ONE安装及配置)
- Arbitrage(bellman,最长路径)
- Unity集成内嵌网页插件-支持Android和IOS
- 【旧代码整理】uri路由处理的重要组成部分 Model_Route
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- cython,加速python,保护代码(3):扩展文件 .pxd
- 四 skylable集群管理
- bootstrap模态框
- xml解析 SAX
- 剑指offer-5-面试36:数组中的逆序对(时间效率和空间效率的平衡)
- Oracle expdp/impdp导出导入命令及数据库备份
- L1-009. N个数求和
- #159 – Creating a Read-Only Dependency Property(创建一个只读的依赖属性)
- 如何开发jQuery自定义插件二