【问题标题】:Files form .py not reading into C文件形式 .py 未读入 C
【发布时间】:2011-04-30 14:22:07
【问题描述】:

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

【问题讨论】:

    标签: python c python-c-api


    【解决方案1】:

    试试这个:

    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);
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-20
        • 1970-01-01
        • 2011-12-19
        • 2010-12-23
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多