python - .py 形式的文件未读入 C

标签 python c python-c-api

假设我有来自 main 的 float x

import progC
def main():
     x = 3
     y=progC(x)
     print y

if __name__ == __main__
     main()
<小时/>
#include <Python.h>

static PyObject* py_bracket(PyObject* self, float x){
    x = x+5;
    return Py_BuildValue("d",x);
}

好的问题是我的 C 程序中的 float x 从 python 接收到 0 而不是数字 3

最佳答案

试试这个:

static PyObject* py_bracket(PyObject* self, PyObject* args){
    float x;
    if (!PyArg_ParseTuple(args, "f", &x))
        return NULL;
    x = x+5;
    return Py_BuildValue("f",x);
}

关于python - .py 形式的文件未读入 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5842342/

相关文章:

python - numpy:gzipped 文件的 fromfile

python - 如何计算 Theano 中的 GPU 内存使用情况?

c - 很难在 C 中将结果相加(For 循环)

php - 通过 PHP 提供 C 程序,无需写入/上传权限

Python C 扩展如果不将借用的引用归还给 Python Land,是否需要 Py_INCREF?

python - 如何使用 SqlAlchemy 增加多行

python - 用 Python 表示图(数据结构)

c - 为什么向 %s 提供指针参数不起作用?

Python C 扩展将 Capsule 暴露给 ctypes 以使用第三方 C 代码

Python C API : when are we at the end of a Python instruction?