【发布时间】:2018-12-01 15:58:53
【问题描述】:
我需要对一个将回调函数作为其输入之一的 fortran90 代码进行 python 绑定。我尝试按照scipy网页上的例子,创建了一个helloworld文件helloworld.f90,如下:
SUBROUTINE helloworld(fun,a,b)
external fun
real*8, intent(in) :: a
real*8, intent(out) :: b
print*, 'Hellow world'
print*, a
b = fun(a)
print*, b
END
我用f2py -c -m hello helloworld.f90 编译这个。编译没有错误,但是python代码没有按预期工作:来自python的函数调用总是返回0(!?):
In [1]: import hello
In [2]: def fun(x): return x**2
In [3]: hello.helloworld(fun,2.)
Hellow world
2.0000000000000000
0.0000000000000000
Out[3]: 0.0
有人明白为什么fun(2.) 在fortran 中被评估为0 吗?我的编译器有问题吗?
【问题讨论】: