[f2py] A mostly working example
James Amundson
amundson@fnal.gov
22 Mar 2002 13:27:32 -0600
On Fri, 2002-03-22 at 13:00, Pearu Peterson wrote:
> >
> > Why does the argument to foo only get modified if it is a Numeric array?
> > Is that the desired behavior?
>
> Yes, and it is the only possible behaviour because Python integers are
> immutable and they cannot modified in situ. Numeric array is mutable and
> it can be modified in situ.
Ah, I see. That wasn't clear to me at all. Thanks.
> However note that using intent(inout) variables are depreciated exactly
> due to this issue. Using intent(in,out) is recommended for
> variables that are both input and output.
>
> Regards,
> Pearu
>
But intent(in,out) doesn't work! Here's the new foo.f:
! Fortran file foo.f:
subroutine foo(a)
integer a
cf2py intent(in,out) a
a = a + 5
end
Here's what happens when I use it:
|abacus>python
Python 2.1.1 (#1, Feb 18 2002, 14:18:11)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-85)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import foobar
>>> from Numeric import *
>>> a = array(1)
>>> print a,foobar.foo(a),a
1 6 1
>>> print foobar.__doc__
This module 'foobar' is auto-generated with f2py
(version:2.13.175-1233).
Functions:
a = foo(a)
bar = bar(a,b)
.
>>>
I only get the correct behavior for foo if I use intent(inout).
Thanks,
Jim