Re: [f2py] intent(inout) scalar arguments
Pearu Peterson <[email protected]> Thu, 28 Apr 2011 08:08:31 +0300
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Apr 28, 2011 at 7:02 AM, Ben Forbes <[email protected]> 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 _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users