[f2py] A mostly working example

Pearu Peterson pearu@cens.ioc.ee
Fri, 22 Mar 2002 21:45:37 +0200 (EET)


On 22 Mar 2002, James Amundson wrote:

> 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).

What you get above _is_ correct. You pass in a value `1' that in Fortran
foo is incremented by `5' and then resulting value `6' is returned to
Python. It is the crucial that the intent(in) argument `a' is _not_
changed in situ - in Python this is generally considered an evil
side-effect.

May be I am missing your point why one would expect anything different
from intent(in,out) than the above?

Pearu