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:

def myfunction(x, y=2):a = x-yreturn a + x * ydef _helper(a):return a + 1class A:def __init__(self, b=0):self.a = 3self.b = bdef foo(self, x):print x + _helper(1.0)

and adds A.pxd:

cpdef int myfunction(int x, int y=*)
cdef double _helper(double a)cdef class A:cdef public int a,bcpdef foo(self, double x)

then Cython will compile the A.py as if it had been written as follows:

cpdef int myfunction(int x, int y=2):a = x-yreturn a + x * ycdef double _helper(double a):return a + 1cdef class A:cdef public int a,bdef __init__(self, b=0):self.a = 3self.b = bcpdef foo(self, double x):print x + _helper(1.0)

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):

    cpdef int myfunction(int x, int y=*)
    

  • C function signatures of internal functions can be declared as cdef:

    cdef double _helper(double a)
    

  • 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相关推荐

  1. cython python3_30倍!使用Cython加速Python代码

    原标题:30倍!使用Cython加速Python代码 作者:George Seif.Thomas Wolf.Lukas Frei 编译:1+1=6 | 公众号海外部 前言 你可能经常会一次又一次地听到 ...

  2. 用Cython加速Python代码,快到起飞!

    点击⬆️"小詹学Python",选择"星标"公众号 重磅干货,第一时间送达 编译 | sunlei 发布 | ATYUN订阅号 本文介绍 如果您曾经用Pytho ...

  3. python备份文件代码_python实现文件的备份流程详解

    python实现输入要备份的文件名称:test.txt 12行代码实现文件备份功能 第一步:打开我们的pycharm软件,然后新建一个Python文件 第二步:新建好我们的Python文件后,我们在编 ...

  4. python转cython_用Cython加速Python到“起飞”(推荐)

    事先声明,标题没有把"Python"错打成"Cython",因为要讲的就是名为"Cython"的东西. Cython是让Python脚本支持 ...

  5. 用Cython加速Python到“起飞”

    Cython-logo 事先声明,标题没有把"Python"错打成"Cython",因为要讲的就是名为"Cython"的东西. Cython ...

  6. python保存代码_python操作文件读写新增保存代码

    file1 = open('scores.txt','r',encoding='utf-8′) #utf8的方式读文件 file_lines = file1.readlines() #取出文件 按行写 ...

  7. python 大项目使用cython_提升6.75倍!利用Cython为Python代码加速

    全文共2012字,预计学习时长4分钟 图片来源:Unsplash 如果你曾经用Python编写过代码,可能会发现等待某些代码块执行的时间比预期要长.尽管可以通过一些方法提高其代码效率,但它的反应速度仍 ...

  8. 将python编程为c_使用Cython为Python编写更快的C扩展

    在我们这个包含了 7 个 PyPI 库的系列文章中学习解决常见的 Python 问题的方法. Python 是当今使用最多的流行编程语言之一,因为:它是开源的,它有广泛的用途(例如 Web 编程.业务 ...

  9. python代码加密cython_利用Cython对python代码进行加密

    利用Cython对python代码进行加密 Cython是属于PYTHON的超集,他首先会将PYTHON代码转化成C语言代码,然后通过c编译器生成可执行文件.优势:资源丰富,适合快速开发.翻译成C后速 ...

最新文章

  1. 可持久化线段树(静态)【学习笔记】
  2. Netflix CEO哈斯廷斯:内容为王 公司潜力依然巨大
  3. Java设置软件图标即窗口上角图标
  4. 今日头条PC端的状态管理,使用的实现库是Redux
  5. python识别人脸多种属性_OpenCV-Python(3)训练一个人脸识别器
  6. Symbol Mc1000 快捷键 的 设置 事件 开发
  7. 内核模块编程之入门(二)—必备知识
  8. 参数调整 新部署rac_CDH5部署三部曲之三:问题总结
  9. Windows 7 God Mode
  10. timesten mysql_TimesTen进程种类详解
  11. ocr初始化失败怎么办_应用程序正常初始化失败
  12. R语言str_c函数
  13. 【杂文】NOIP2018 蒟蒻自闭记
  14. 阿里IOT用AMQP在服务端订阅消息,踩坑
  15. 创客匠人用户分组:用户定向运营,不浪费一滴流量
  16. Python周刊505期
  17. 高通平台关机闹钟设置
  18. Redis 持久化——RDB 详解
  19. 英语口语8000句-随意的谈话
  20. 国防科大 linux教程,操作系统国防科大罗宇

热门文章

  1. 一款牛逼的Java工具类库,GitHub星标10.7k+,你敢用吗?
  2. freemind中文方框解决
  3. pandas删除满足多条件的行
  4. ORACLE子查询的多种用法
  5. 如何使用Delphi 10 Seattle的Android应用做Intent的发送和接收
  6. 【困扰了很久,实测已解决】MacBook上不了V2EX网站但Windows可以
  7. 生活,寻找幸福的感觉
  8. 当当网资深DBA:DB运维四大现代化的实现
  9. 30天,O2O速成攻略【7.18广州站】
  10. 电子表格转web程序控件SpreadsheetWeb v6.0史上最大版本发布 | 附下载