用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](extending.xhtml "1. 使用 C 或 C++ 扩展 Python") | - [上一页](../library/undoc.xhtml "未创建文档的模块") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - $('.inline-search').show(0); | # 扩展和嵌入 Python 解释器 本文档描述了如何使用 C 或 C++ 编写模块以使用新模块来扩展 Python 解释器的功能。 这些模块不仅可以定义新的函数,还可以定义新的对象类型及其方法。 该文档还描述了如何将 Python 解释器嵌入到另一个应用程序中,以用作扩展语言。 最后,它展示了如何编译和链接扩展模块,以便它们可以动态地(在运行时)加载到解释器中,如果底层操作系统支持此特性的话。 本文档假设你具备有关 Python 的基本知识。有关该语言的非正式介绍,请参阅 [Python 教程](../tutorial/index.xhtml#tutorial-index) 。 [Python 语言参考](../reference/index.xhtml#reference-index) 给出了更正式的语言定义。 [Python 标准库](../library/index.xhtml#library-index) 包含现有的对象类型、函数和模块(内置和用 Python 编写)的文档,使语言具有广泛的应用范围。 关于整个 Python/C API 的详细介绍,请参阅独立的 [Python/C API 参考手册](../c-api/index.xhtml#c-api-index) 。 ## 推荐的第三方工具 本指南仅介绍了作为此 CPython 版本的一部分提供的创建扩展的基本工具。 第三方工具,如 [Cython](http://cython.org/) \[http://cython.org/\] 、 [cffi](https://cffi.readthedocs.io) \[https://cffi.readthedocs.io\] 、 [SWIG](http://www.swig.org) \[http://www.swig.org\] 和 [Numba](https://numba.pydata.org/) \[https://numba.pydata.org/\] 提供了更简单和更复杂的方法来为 Python 创建 C 和 C ++ 扩展。 参见 [Python Packaging User Guide: Binary Extensions](https://packaging.python.org/guides/packaging-binary-extensions/) \[https://packaging.python.org/guides/packaging-binary-extensions/\]“ Python Packaging User Guide ”不仅涵盖了几个简化二进制扩展创建的可用工具,还讨论了为什么首先创建扩展模块的各种原因。 ## 不使用第三方工具创建扩展 本指南的这一部分包括在没有第三方工具帮助的情况下创建 C 和 C ++ 扩展。它主要用于这些工具的创建者,而不是建议你创建自己的 C 扩展的方法。 - [1. 使用 C 或 C++ 扩展 Python](extending.xhtml) - [1.1. 一个简单的例子](extending.xhtml#a-simple-example) - [1.2. 关于错误和异常](extending.xhtml#intermezzo-errors-and-exceptions) - [1.3. 回到例子](extending.xhtml#back-to-the-example) - [1.4. 模块方法表和初始化函数](extending.xhtml#the-module-s-method-table-and-initialization-function) - [1.5. 编译和链接](extending.xhtml#compilation-and-linkage) - [1.6. 在C中调用Python函数](extending.xhtml#calling-python-functions-from-c) - [1.7. 提取扩展函数的参数](extending.xhtml#extracting-parameters-in-extension-functions) - [1.8. 给扩展函数的关键字参数](extending.xhtml#keyword-parameters-for-extension-functions) - [1.9. 构造任意值](extending.xhtml#building-arbitrary-values) - [1.10. 引用计数](extending.xhtml#reference-counts) - [1.11. 在C++中编写扩展](extending.xhtml#writing-extensions-in-c) - [1.12. 给扩展模块提供C API](extending.xhtml#providing-a-c-api-for-an-extension-module) - [2. 自定义扩展类型:教程](newtypes_tutorial.xhtml) - [2.1. 基础](newtypes_tutorial.xhtml#the-basics) - [2.2. Adding data and methods to the Basic example](newtypes_tutorial.xhtml#adding-data-and-methods-to-the-basic-example) - [2.3. Providing finer control over data attributes](newtypes_tutorial.xhtml#providing-finer-control-over-data-attributes) - [2.4. Supporting cyclic garbage collection](newtypes_tutorial.xhtml#supporting-cyclic-garbage-collection) - [2.5. Subclassing other types](newtypes_tutorial.xhtml#subclassing-other-types) - [3. 定义扩展类型:已分类主题](newtypes.xhtml) - [3.1. 终结和内存释放](newtypes.xhtml#finalization-and-de-allocation) - [3.2. 对象展示](newtypes.xhtml#object-presentation) - [3.3. Attribute Management](newtypes.xhtml#attribute-management) - [3.4. Object Comparison](newtypes.xhtml#object-comparison) - [3.5. Abstract Protocol Support](newtypes.xhtml#abstract-protocol-support) - [3.6. Weak Reference Support](newtypes.xhtml#weak-reference-support) - [3.7. 更多建议](newtypes.xhtml#more-suggestions) - [4. 构建C/C++扩展](building.xhtml) - [4.1. 使用distutils构建C和C++扩展](building.xhtml#building-c-and-c-extensions-with-distutils) - [4.2. 发布你的扩展模块](building.xhtml#distributing-your-extension-modules) - [5. 在Windows平台编译C和C++扩展](windows.xhtml) - [5.1. A Cookbook Approach](windows.xhtml#a-cookbook-approach) - [5.2. Differences Between Unix and Windows](windows.xhtml#differences-between-unix-and-windows) - [5.3. Using DLLs in Practice](windows.xhtml#using-dlls-in-practice) ## 在更大的应用程序中嵌入 CPython 运行时 有时,不是要创建在 Python 解释器中作为主应用程序运行的扩展,而是希望将 CPython 运行时嵌入到更大的应用程序中。 本节介绍了成功完成此操作所涉及的一些细节。 - [1. Embedding Python in Another Application](embedding.xhtml) - [1.1. Very High Level Embedding](embedding.xhtml#very-high-level-embedding) - [1.2. Beyond Very High Level Embedding: An overview](embedding.xhtml#beyond-very-high-level-embedding-an-overview) - [1.3. Pure Embedding](embedding.xhtml#pure-embedding) - [1.4. Extending Embedded Python](embedding.xhtml#extending-embedded-python) - [1.5. Embedding Python in C++](embedding.xhtml#embedding-python-in-c) - [1.6. Compiling and Linking under Unix-like systems](embedding.xhtml#compiling-and-linking-under-unix-like-systems) ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](extending.xhtml "1. 使用 C 或 C++ 扩展 Python") | - [上一页](../library/undoc.xhtml "未创建文档的模块") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。