# 使用C扩展

CPython 还为开发者实现了一个有趣的特性，使用 Python 可以轻松调用 C 代码。

开发者有三种方法可以在自己的 Python 代码中来调用 C 编写的函数──`ctypes`，`SWIG`，`Python/C API`。每种方式也都有各自的利弊。

首先，我们要明确为什么要在 Python 中调用 C？

常见原因如下：

* 你要提升代码的运行速度，而且你知道 C 要比 Python 快50倍以上；
* C 语言中有很多传统类库，而且有些正是你想要的，但你又不想用 Python 去重写它们；
* 想对从内存到文件接口这样的底层资源进行访问；
* 不需要理由，就是想这样做。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eastlakeside.gitbook.io/interpy-zh/c_extensions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
