设为首页 收藏本站
查看: 678|回复: 0

[经验分享] (Python编程)C扩展概览

[复制链接]

尚未签到

发表于 2017-4-22 13:55:30 | 显示全部楼层 |阅读模式
Programming Python, 3rd Edition 翻译
最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD

22.3. C Extensions Overview

22.3. C扩展概览

Because Python itself is coded in C today, compiled Python extensions can be coded in any language that is C compatible in terms of call stacks and linking. That includes C, but also C++ with appropriate "extern C" declarations (which are automatically provided in Python header files). Regardless of the implementation language, the compiled Python extensions language can take two forms:

因为现在的Python本身是用C语言写的,编译型Python扩展可以用任何C兼容语言编码,只要其调用堆栈与链接方式与C语言兼容。那包括C和C++,但C++需要适当地声明"extern C"(在Python头文件中已自动提供)。不管用什么语言实现,编译型Python扩展语言可采用两种形式:

C modules
These look and feel to their clients like Python module files
C types
These behave like standard built-in types (numbers, lists, and so on)

C模块
对客户的感观就像是Python模块文件
C类型
它们的行为表现像是标准的内建类型(如数字,列表等等)

Generally, C extension modules are used to implement flat function libraries, and they wind up appearing as importable modules to Python code (hence their name). C extension types are used to code objects that generate multiple instances and may optionally support expression operators much like classes. Because built-in types are really just precoded C extension types, your C extension types can do anything that built-in types can: method calls, addition, indexing, slicing, and so on.


  • 通常,C扩展模块用来实现单一的函数库,它们对Python代码像是可导入模块(所以叫扩展模块)。C扩展类型用来编写对象,对象可生成多个实例,可以支持表达式操作符,就像类。因为内建类型实际上只是预编码的C扩展类型,你的C扩展类型能做到任何内建类型所能做的事:如方法调用,加法,索引,切片,等等。


  • In fact, every time you make an integer or string in Python, you generate a new C type instance object (whether you know it or not). This isn't as inefficient as you may think, though; as we'll see, type operations are dispatched through fast C pointers, and Python internally caches some integers and strings to avoid object creation when possible.

  • 实际上,每次你用Python构造一个整数或字符串,就产生一个新的C类型的对象实例(不管你是否知道)。但是这并不像你所想的那么低效;我们将会看到,类型操作是通过快速的C指针分派的,并且Python会内部缓存一些整数和字符串来尽力避免创建对象。

    Moreover, C extension types today may provide a class-like interface, and so can support customization by either subclassing or coding "wrapper" classes as frontend interfaces to the type. For instance, as we saw in Chapter 20, the Python list object may now be customized by direct subclassing.

    此外,C扩展类型现在可以提供一个像类的接口,并能进行定制,如用继承或编写“包装”类作为最后的类型接口。例如,我们在第20章看到,Python list对象现在能通过直接继承来定制。

    To make the interface work, both C modules and types must provide a layer of "glue" code that translates calls and data between the two languages. This layer registers C-coded operations with the Python interpreter as C function pointers. In all cases, the C layer is responsible for converting arguments passed from Python to C form and for converting results from C to Python form. Python scripts simply import C extensions and use them as though they were really coded in Python. Because C code does all the translation work, the interface is very seamless and simple in Python scripts.

    为了让接口工作,C模块与类型都必须提供一层“粘合”代码,来翻译两种语言之间的调用与数据。这一粘合层在Python解释器中将C编码的操作注册为C函数指针。C语言层负责把Python参数转化为C语言的形式,并将C语言的结果转化为Python的形式。Python脚本只需简单地导入C扩展就可使用它们,就像它们是Python编码的。因为C代码做了所有的翻译工作,Python脚本中的扩展接口是非常简单的并且无缝的。

    C modules and types are also responsible for communicating errors back to Python, detecting errors raised by Python API calls, and managing garbage-collector reference counters on objects retained by the C layer indefinitelyPython objects held by your C code won't be garbage-collected as long as you make sure their reference counts don't fall to zero. Once coded, C modules and types may be linked to Python either statically (by rebuilding Python) or dynamically (when first imported). Thereafter, the C extension becomes another toolkit available for use in Python scripts.

    C模块与类型也负责向Python回传错误,检测Python API调用引发的错误,并对C语言层保留的不确定的对象进行引用计数和垃圾收集的管理。你的C代码持有的Python对象不会被垃圾收集,只要你确保它们的引用计数不降为零。一旦编码,C模块和类型可以静态链接Python(通过重编译Python),也可以动态链接(即在初次导入时)。之后,C扩展就变成可在Python脚本中使用的又一个工具箱。

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-367849-1-1.html 上篇帖子: Python,简单的力量 下篇帖子: Python排序算法
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表