我是否需要 SWIG 类型映射才能使 c 函数 return 浮点到 python?

Do I need a SWIG typemap to have a c function return a float to python?

我正在尝试从 python 调用 C 函数。此函数将多个数组作为输入,returns 一个浮点数。

我需要 SWIG 类型映射来执行此操作吗?一个问题是 python 没有区分 floatsdouble 等,我特别感兴趣的是只返回 c 类型 float.

返回 float "just works"。您不需要额外的类型映射:

test.i

%module test

%inline %{
    float func(void) { return 1.5; }
%}

运行 swig 并编译结果后:

>>> import test
>>> test.func()
1.5