在模块中组合C和Python函数

Jam*_*kin 0 python cpython

我有一个C扩展模块,我想添加一些Python实用程序函数.有推荐的方法吗?

例如:

import my_module

my_module.super_fast_written_in_C()
my_module.written_in_Python__easy_to_maintain()
Run Code Online (Sandbox Code Playgroud)

我主要对Python 2.x感兴趣.

Joh*_*hin 7

通常的做法是:mymod.py包含用Python编写的实用程序函数,并导入_mymod模块中的好东西,该模块用C编写并从_mymod.so或_mymod.pyd导入.例如,查看Python发行版中的.../Lib/csv.py.


Mik*_*cki 5

使用下划线为您的原生扩展名添加前缀.然后,在Python中,创建一个包装器模块,该模块导入该本机扩展并在其上添加一些其他非本机例程.