[f2py] intent(inout) scalar arguments

Pearu Peterson pearu.peterson at gmail.com
Thu Apr 28 08:08:31 EEST 2011


On Thu, Apr 28, 2011 at 7:02 AM, Ben Forbes <bdforbes at gmail.com> wrote:

> I've read section 4.1 "Scalar arguments" in the documentation, but I
> still don't understand why rank-0 arrays must be used. If I have a
> routine:
>
> subroutine foo(b)
>  integer(4),intent(inout)::b
>  b=b+10
> end subroutine foo
>
> and I call from Python using
>
> b=5
> scalar.foo(b)
>
> Won't a pointer to b get passed through to the C-wrapper and then to
> Fortran? Why wouldn't b be incremented?
>

Short answer: Python scalars are immutable objects, hence they cannot be
changed in-situ.

To extend this, if f2py generated module or any extension module would
change Python int object in situ, then there will be global consequences.
Namely, in Python small integer objects are cached and let's say that
scalar.foo would increment `b` in-situ, then the following will happen:

b = 5
scalar.foo(b)
print b
--> 15 # this looks ok, but
print 5
--> 15 # because the integer object corresponding to `5` contains C int
value `15`

I hope this example will satisfy your curiosity. If something is still
unclear, then you have to learn the implementation details of Python
objects. The best way to learn that is to go to Python source and read it.

Regards,
Pearu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cens.ioc.ee/pipermail/f2py-users/attachments/20110428/1a87aeea/attachment.htm 


More information about the f2py-users mailing list