I wrapeed up a fortran code using f2py and called the module twice in a python code. But some variable values in the fortran code seems to be inheritated between these two calls.
I understand that the fortran variable declarations would only be executed once. But why would the variable values be saved between two calls of the module? Is there any way that I can reset the fortran module every time I call it in python?
Here is a test code, and the actual fortran code is much more complicated, so it may be impossible for me to simply clear some variables.
fortran code:
subroutine test
integer::aa=1
write(*,*) aa
aa=aa+1
end subroutine test
python code:
import test
test.test()
test.test()
the output will be :
1
2
the expected output should be :
1
1
Again that I cannot simply use the
integer::a
a=1
Because I have too many subroutines in my actual fortran code, so it is hard to check every variable declaration. So I wonder if there is any wiser way to crack this problem.
EDIT: summary of the comments till now:
python import the fortran module as a library and it will remain in the memory between two calls. No useful "reload" methods reported yet.
fortran has the implicit save problem as mentioned before. cs.rpi.edu/~szymansk/OOF90/bugs.html here is a useful link provided by @Oo.oO
And there is a new relevant question about the usage of the fortran module in parallel computation. As mentioned by @Pierre de Buyl, the module generated through f2py is not threadsafe. I am new to python and wonder if it is able to use the module in python's "multiprocessing". (If this question is considered to be irrelevant, I will remove it.)
EDIT2: Recently I tried the "data" command to assign values. And i find that it has the same implicit save problem as mentioned before.
integer::a
data a/1/
a=a+1
write(*,*) a
results:
2
3
Used to think the problem just occured in command like "integer::a=1".