How can I expose callbacks to Fortran using Python

0 votes

[Callback functions] may also be explicitly set in the module. Then it is not necessary to pass the function in the argument list to the Fortran function. This may be desired if the Fortran function calling the python callback function is itself called by another Fortran function.

That's what has been stated in the scipy documentation. However, I can't seem to find an example of how this would be done.

Let's take this particualr Fortran / Python combination for an example:

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

call_test.py:

import test

def func(x):
    return x * 2

test.test(func)

Compiled with the following command (Intel compiler):

python f2py.py -c test.f --fcompiler=intelvem -m test

What changes would I have to take to expose the function to the entire Fortran program in the form of a module, so that I could call the function from inside the subroutine test, or any other subroutine in any other fortran file in the project?

Aug 23, 2018 in Python by charlie_brown
• 7,720 points
125 views

1 answer to this question.

0 votes

The code that I've written below. The important thing to note here is the absence of any parameters passed to test.

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

As an aside, I then wrapped py_func in a subroutine so that I could call it without having to declare the following in every file / function I use it:

integer y
y = py_func(x)
answered Aug 23, 2018 by ariaholic
• 7,340 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 3,060 points
75 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,704 views
+2 votes
2 answers

How can I plot a k-dsitance graph using python?

Hi there, instead of sklearn you could ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
1,173 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 229 views
+4 votes
6 answers
0 votes
2 answers

How can I install external binaries using portable python

0110100001000101001010101001011010100100111100101001 READ MORE

answered Jan 24 in Python by anonymous
123 views
0 votes
1 answer

How can I rename files on the fly using Python?

You could simply use a wrapper object ...READ MORE

answered Sep 7, 2018 in Python by ariaholic
• 7,340 points
41 views