Jim*_*Joo 5 python fortran module f2py
[回调函数]也可以在模块中明确设置.然后,没有必要将参数列表中的函数传递给Fortran函数.如果调用python回调函数的Fortran函数本身由另一个Fortran函数调用,则可能需要这样做.
但是,我似乎无法找到如何做到这一点的例子.
考虑以下Fortran/Python组合:
test.f:
subroutine test(py_func)
use iso_fortran_env, only stdout => output_unit
!f2py intent(callback) py_func
external py_func
integer py_func
!f2py integer y,x
!f2py y = py_func(x)
integer :: a
integer :: b
a = 12
write(stdout, *) a
end subroutine
Run Code Online (Sandbox Code Playgroud)
call_test.py:
import test
def func(x):
return x * 2
test.test(func)
Run Code Online (Sandbox Code Playgroud)
使用以下命令编译(英特尔编译器):
python f2py.py -c test.f --fcompiler=intelvem -m test
Run Code Online (Sandbox Code Playgroud)
我需要采取哪些更改才能以func模块的形式暴露给整个Fortran程序,以便我可以从子程序内部调用函数test,或者在项目中的任何其他fortran文件中调用任何其他子程序?
以下内容对我有用。请注意,没有传递给测试的参数。python 文件如问题中所述。
subroutine test()
use iso_fortran_env, only stdout => output_unit
!f2py intent(callback) py_func
external py_func
integer py_func
integer y,x
!f2py y = py_func(x)
integer :: a
integer :: b
a = 12
write(stdout, *) a
end subroutine
Run Code Online (Sandbox Code Playgroud)
顺便说一句,然后我封装py_func了一个子例程,以便我可以调用它,而不必在我使用它的每个文件/函数中声明以下内容:
integer y
y = py_func(x)
Run Code Online (Sandbox Code Playgroud)