0

假设我有来自 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

4

2 回答 2

0

在我看来,这个问题可以通过使用int代替来解决float。Python 变量默认保存 12 字节的内存,而“C float”仅保存 4 个字节。第二个选项是使用PyTypeObject PyFloat_Type指定float类型是必需的。

于 2011-04-30T14:32:41.573 回答
0

尝试这个:

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);
}
于 2011-04-30T14:34:18.933 回答